简体   繁体   中英

Entity Framework 4 relating tables with no foreign key constraint

I am working with Entity Framework for the first time. The database that I am working with has no foreign key constraints defined (and I cannot change that, integrity is controlled at the app level, out of my hands).

There is a very strong convention in the database -- all PK's are named a specific way, and an FK field has exactly the same name as its corresponding PK.

When I draw a relation with EF, it seems to want to auto-create an FK field in the child table. Is there any way I can teach EF to infer our relations based upon our convention?

There is nothing built-in to infer FKs based on naming conventions, but you can manually add the associations to your model. Associations don't need an underlying FK constraint so you can add the associations to the model only...


Update: In the latest version of the Model Comparer for EF4 , I added a feature for inferring FKs and generating associations in EFv4 models based on inferred FK constraints. Blogged about it here: http://huagati.blogspot.com/2010/12/inferring-foreign-key-constraints-in.html

I hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM