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

僤鯓⒐⒋嵵緔 提交于 2019-12-02 06:39:36

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.

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