EF 4 Entity Model design question
I have an existing data model that I created which has no data in it yet. Using Visual Studio 2010 I generated my .edmx file (entity data model) from this database. I feel like there are probably several FK's in my DB that I do not need in my entity model.
What is the best practice for determining needed Navigation Properties in my entity data model? What should I be thinking about when I look at each entity and try to determine if the Navigation Property is needed or not.
If I do not need a certain Navigation Property should I also delete that FK in my data model as well so that future Entity Model updates do not pul开发者_运维问答l them back in?
Also what is a best practice when determining if an FK is needed or not in my data model.
Why did you introduce FKs to database? You probably used them to estabilish some referential integrity, didn't you? In such case leave your FKs in DB and let EF to include them in your model.
EDMX file has several parts - storage model, conceptual model and mapping between models (it also have information about designer but it is not interesting). When you add tables into EDMX it will describe them in storage model together with all FKs (associations). It also creates associations in conceptual model and adds navigation properties to entities.
Updating from database do not affect your changes to conceptual model (entities). If you remove navigation property it will not be added next time you update your model but FK will still exist.
Decission to leave navigation property or remove it is hard to make up front. You will see it during development of your application. It can also be introduced by some rules in your domain model. Sometimes you need to write complex Linq-to-entities query. Navigation properties are quite handy when writing queries because thay can save you of writting a lot of left joins in Linq.
精彩评论