Reputation: 22551
As far as I can tell (correct me if I'm wrong), there are two main approaches to using Entity Framework:
I have an existing database and I'd like to write the code myself. Is this "Code Only" approach supported? Does such an approach even make sense in the context of EF?
Upvotes: 5
Views: 348
Reputation: 6050
The Entity Framework team answered these questions on their blog: http://blogs.msdn.com/b/adonet/archive/2011/03/07/when-is-code-first-not-code-first.aspx
Upvotes: 1
Reputation: 156624
I disagree with most of the other answers. From what I've seen, the EF "Code First" technology is really just a way to define your model using conventions, annotations, or a fluent mapping definition, rather than an EDMX file. If you write your "Code First" files to mirror your database schema, there is no reason that Entity Framework would be unable to produce the appropriate queries and statements using LINQ to Entities.
For more information, see Scott Guthrie's post on Using EF "Code First" with an existing database.
Upvotes: 5
Reputation: 52083
Entity Framework Power Tools allows you to reverse engineer a database to generate code first like code (that won't re-generate your database). Then you can tweak it from there as you need.
Upvotes: 2
Reputation: 160942
Well I guess you can't have your cake and eat it too in this case - there has to be one definite source on what your model is, it is either the database (DB first), which then generates matching code for you, or the code (Code first) which will then create a matching DB.
Upvotes: 1
Reputation: 15513
I believe you have to decide what your system of reference is -- the code (Code First) or the database (Model First). If you have an existing database, then go with a Code First approach, it will be hard to keep your changes in synch without generating your model from your code, or your code from your model.
If you have an existing database, but want to extend your model beyond the generated code, you could implement partial classes to accomplish this.
If you want to manually map your EF4 code and your database, you could consider this approach. However, this eliminates some of the benefit of an ORM, which is to set up the mapping for you.
Upvotes: 1