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?
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.
Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…
As clear from the figure, the entity framework creates or updates the database depending upon the domain classes. Hence, the user needs to code first, and then the entity framework will create the database using the code. That is why it is called the code first approach.
Database-First Approach of Entity Framework: We can use the Database First Approach if the database schema already existing. In this approach, we generate the context and entities for the existing database using the EDM wizard. This approach is best suited for applications that use an already existing database.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With