Map tables relationship from legacy database w/ entity framework with only primary keys

回眸只為那壹抹淺笑 提交于 2020-01-11 12:13:51

问题


data (table name)

dataid PK,

value1,

value2,

value3

data_address (table name)

dataaddressid PK,

dataid - id to errenddataid,

addressid1 - id to en addressid,

addressid2 - id to en addressid,

type

address (table namne)

addressid PK - id to addressid1 or addressid2,

address1,

address2,

name,

zipcode,

city

I have a really hard time trying to map this relations using Entity Framework 5, if some one have an idea or good links I would much appreciate that!


回答1:


If you are certain that the database's integrity is sound you could just map the tables and create the associations manually in the EF model.

In a database-first mode I fiddled a bit with a simple data model: Parent + Child without FK. The tables were of course imported without association between them. Then I did "Add Association..." on the Parent, like so:

Note: no foreign key property yet. I added it manually in the properties of the association:

And I could run a linq query on Parent.Children.

I think this is the easiest way for you. The edmx design surface gives you some guidance to see which associations you created. You can always add a code generation item to generate a DbContext which is easiser to work with than the default ObjectContext.



来源:https://stackoverflow.com/questions/12581835/map-tables-relationship-from-legacy-database-w-entity-framework-with-only-prima

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!