Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Association in Entity Framework?

I have a database in which the tables do not have primary/foreign 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?

like image 873
Dave Mackey Avatar asked Aug 09 '26 20:08

Dave Mackey


1 Answers

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.

like image 54
Ladislav Mrnka Avatar answered Aug 11 '26 10:08

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!