Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 682
KallDrexx Avatar asked May 23 '10 03:05

KallDrexx


People also ask

How do you implement code first approach in Entity Framework?

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…

Why We Use code First approach in Entity Framework?

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.

Which approach is best in Entity Framework?

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.


1 Answers

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.

like image 78
KallDrexx Avatar answered Oct 11 '22 01:10

KallDrexx