EF6.1 is closing in on release. Along with the new DLLs that you can get via Nuget, there is a new designer that you can install into Visual Studio 2012 or 2013 via an MSI install.
Before installing, I uninstalled the EF Power Tools, because I wasn’t sure if they were included. Post install of the 6.1 Beta EF Designer, there was no indication of the power tool features in my context menu where it would normally be.
So it looks like this iteration of the designer won’t have the power tool features wrapped in except for the BIG one, the replacement of the reverse engineer into code first. (So don’t uninstall those power tools! You can still use the other features like this one: my favorite).
With the new designer, when you create a new model, you now get 4 options. Two are familiar, EF Model from Database and Empty Model. The new ones are Empty Code First Model and Code First from database.
I chose Code First from database and pointed to AdventureWorks2012 and selected everything in the Human Resources schema.
This resulted in all of my new classes being created in my project, not in a folder.
Also, it added references to EF.
But the classes felt a little messy. So for my next model, I began with a new project folder. (note that I am only experimenting. I do not recommend having two models in the same project.)
Then I added the new entity model to th efolder, not the outer project. This time I selected the Person schema.
and all the new classes landed in my new folder
I prefer that. In a real app I would then separate the domain classes into their own projects and I would also put each of the models in its own project.
Let’s take a look at the domain classes that this generated.
Notice that tehre are data annotations. This is different than the EF Power Tools which put everything in fluent API code.
But those are annotations that are reasonable in a class since they are not solely defining a database mapping strategy.. required and stringlength can also be used by EF’s and .NET’s validation APIs. For the latter , think MVC validation.
But that’s not how this tool decides to use data annotations. Check the BusinessEntityAddress type.
It has annotations that drive the composite key creation and also that note database generated mappings. I wouldn’t normally put those in my domain types.
The rule that is being used by default is: any mappings that can be described with a data annotation are done that way. Everything else is done with a fluent api. None of my tables triggered any fluent api mappings. There is some discussion (started by guess who? ) on codeplex about making it easier to force the designer to use all fluent mappings if you want.
I was curious what Empty Code First Model gives us. It sets up a shell DbContext class. Makes sense to me.
I have been asking for the ability to select individual tables/views for reverse engineering an existing database into Code First context ad classes since the first public CTP of the EF Power Tools. (Here’s proof!) So even with a few things I’d like to see done differently, I am thrilled with this new designer feature.
Why?
For people building new apps with legacy databases, it is a great first step towards setting up DbContexts that you can align with Domain Driven Design Bounded Contexts. It gives me a nice stake in the ground to thinking within boundaries and also forcing me to think about when a customer in one context should be designed differently than a customer type in another context because they are used differently.
Any-hoo…there’s a quick first look for you. There’s still time to provide feedback to the team on the codeplex site. Go to this workitem: https://entityframework.codeplex.com/workitem/407
Also, as Erik reminds me in the comments, there is a Visual Studio extension that provides some more extensive reverse engineer features into code first dbcontext and classes. You can find the docs, a video and the download here: EntityFramework Reverse POCO Generator
Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!
Maybe you should have a look at: https://efreversepoco.codeplex.com
Thanks Erik. I know you’ve pointed me to this before. It’s a great tool and does one up this for sure. But the new EF Designer certainly merited a look-see. Perhaps I should have mentioned it in the blog post as an alternative. I’l do that now.
Too bad you did not describe how to get to the menu’s:
“With the new designer, when you create a new model, you now get 4 options.”
How do I get there?
There are few bloggers who describe how to get there, this is just 1 line of text but I always keep searching for that stuff for hours …
In the future, would be very kind to just describe very simple how to get there, for examle:
“(RichtClick on Project and choose new Model)”
Just 1 line of text but it makes all the difference… for me tough.
BTW “right click on project and choose new model” is not at all how to create a new model.
The process is the same as it has been since 2008:
Right click on project, add, new item, ADO.NET Entity Data Model.
Nice artigle tough!
Sorry Nicolas. The post was not meant to be about how to create an entity framework model but just about looking at what the new feature adds to the wizard that has not changed since VS2008 SP1. Though I’m sure some people who come across this post who have never created an EF model using the wizard before will appreciate your addition, so thanks. Here is an old video I did that demonstrates using the wizard, though it is before the code first option was added: http://msdn.microsoft.com/en-us/data/ff191186.aspx
Julie,
After generating the code first models for an existing database, and Since the _dbo.MigrationHistory does not exist, Should we need to Enable Migrations first and then An Create Initial Create ?, so that for any new changes to the existing models and adding new models , we can then use normal migrations from there on. please suggest how to go about this ?
@Raghav,
After migrations are enabled, the first update-database will create the __MigrationHistory table.
I have your book, “Programming Entity Framework,” but have never used Entity Framework at any of my jobs. I’m in between jobs now and am finally starting to see it more and more in the job descriptions. Where should I start if I were starting from scratch with EF? Should I bother rereading the book,”Programming Entity Framework,” or skip it and start with “DbContext,” or …?
I’d start with my pluralsight course “getting started with Entity Framework 5” . The getting started basics don’t really change between EF5 & EF6. That will point you to other content for drilling into more advanced topics. Let me know if you need me to send you a 30 day trial for pluralsight so you can watch it. Of you have the 2nd edition of the book, there is still a lot of relevant stuff in there e.g. the chapters on querying, on “real world” and some others.
The Code First and DbContext books are still very relevant and useful. Code FIrst was written before Migrations feature (added in EF 4.3) was added and mapping of stored procs (added in EF6) but otherwise, they are both still totally useful!
Julie, nice post.
I already have a database model so I choose Code First from Database and next scaffolded the tables.
But from now on I don’t want to use Code First. Instead I want to update the models based on database changes. What to do next?
I ask because is very important to us keep database design (using Toad DM for SQL Server) always updated.
Thanks in advance
Database First to EDMX is as important as ever. We just have more options now. So you don’t need to do anything. Just keep working with the EDMX and leverage the “update model from database” designer feature. That has remained consistent since the first version of EF and continues to work.
I’m really confused on how to implement data annotations on my entities created from a database-first approach. I found what look like my entities under the “MyModel.tt” node in /Models, but they are partial classes which have a disclaimer comments at the top about them being auto-generated so don’t modify them. This leaves creating another partial class and defining the annotations there. Is this the correct way to do this?
My favorite blog post on Adding data annotations to partial classes is by Steve Smith here: http://ardalis.com/adding-attributes-to-generated-classes Hope it helps!
Perfect; thanks so much.