Are there any good resources for developing Entity Framework 4 code-first?
I am trying to convert my model-first project to code-first, as I can see dealing with the models with the graphical designer will become hard. Unfortunately, with all my googling I can't find one good reference that describes how to do code-first development. Most resources开发者_开发百科 are out of date (so out of date they refer to it as code-only), and the other references I can find seem to assume you understand the basics of context building and code-first (for example, they reference code to build contexts but don't describe where that code should actually go, and how it's actually run).
Are there any decent resources for code-first development, that describe how to map your POCO entities into a database schema?
Edit: Ok well, I give up on code first for now. The getting started document I linked to in my answer seems to have some flaws in it (for example, some details of context building and the EntityConnection dbConnection type, and some of the relationship mapping code does not compile they way they have written it). I spent 2 days trying to find any way to get this to work, and I got close (I think) but ultimately failed with google completely being unhelpful.
As of right now, I think it's more productive for me to keep going with Model first development, and hope that by the time the edmx becomes awkward code-first is no longer in CTP state, and has better (as in some that actually exists) documentation.
I am marking the answer on POCOs as the correct one, as while it isn't actually code-first, if you want to have entities in code it is probably the best bet, even though it requires a database and an EDMX generated from it before it will work.
Well it seems I've gotten a bit closer.
It seems like Code-First was not included in the final released version of VS2010. Thus in order to use it you have to download the Entity Framework CTP 3 at http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&displayLang=en. That seems to be the latest CTP for EF.
After installing that you must add a reference to Microsoft.Data.Entity.CTP.
Finally, that CTP download page brought me to http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx. While the date on the page was June of 2009, it claims it's been updated and the CTP (released in Feb) points to it so it might still be accurate. I'll play with it tomorrow to see how accurate it really is.
Enjoy the new greatest EF CTP 4. There is a lot of new improvements and some good resources to learn a lil bit more.
http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4announcement.aspx
http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4piwalkthrough.aspx
http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4codefirstwalkthrough.aspx
this maybe what your after.
Effectively you just use your EF model to define the mappings to the DB and not to generate classes, and create a context to link your objects to the DB through the EF model mappings.
Adding a new answer as an FYI.
With the latest Code-first CTP and Scott Gu's tutorials on it (found here), I got my model-first system converted to code-first in no time flat.
I don't know how much things will change with the API, but there is at least good information now to get started with code first.
The demos on the ASP.NET MVC site here offer some excellent examples of code first. At 5. Models (Data) the demo itself is called using Entity Framework Code first.
精彩评论