Entity Framework Code-First Benefits when using existing database
Just started looking into Entity Framework and the new Code-First features. My questio开发者_JAVA百科n is this: If I am starting an application and using an existing database, what are the benefits of the Code-First approach as opposed to using EF to map all of the classes for me?
Code-First, as the name implies, is designed to allow you to start from Code and generate the data store (commonly a database schema) from it. If you have an existing database then I would say your options are to auto-generate the entities using the supplied tools or to start with POCOs (Plain Old CLR Objects) that are then mapped to your database. As to which and when unfortunately leads to the age old answer "It depends".
Where the existing database schema reflects the entity graph well and you aren't expecting to pass the entities around a great deal outside of your app then the auto-generate may be a good place to start. Particularly if you are new to EF.
If you are expecting to create a lot of aggregate entities, use inheritance and/or pass the entities around a lot between processes then POCOs may be your best starting point.
My examples are far from comprehensive or definitive so learn about the approaches and then see what feels right. Whatever you decide, you can do a lot worse than start by reading Julie Lerman's book "Programming Entity Framework". The second edition covers EF4.
精彩评论