开发者

Create Association in Entity Framework?

I have a database in which the tables do not have primary/fore开发者_StackOverflowign keys. This database is third party, so I don't really want to edit the underlying database structure. Instead, I've created an EF model which has the tables I want to work with...now I'd like to at the model level define the associations between different entities. For example, I have a Person object and a Demographic object, they share a people_code_id column which will be identical...but I can't seem to figure out how to get this association to show up and then how to be able to access these additional columns via EntityDataSource?


First you must define primary keys in your entity model. Each entity must have primary key or you will not be able to define relation as well as all your entities become read-only. Once you have primary keys defined and your related tables contains foreign key properties (just id of related entity's PK) you can create associations manually. Here is general approach how to do that. It should work even if you don't have relations defined in DB.


EF won't show associations unless they are defined in the database schema. You can still used the keys to do joins like you would in plain SQL to match the keys and their references.

I you added constraints to the DB schema, you wouldn't need to change the tables themselves and it should be effortless unless you already have constraint problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜