Wednesday, March 25, 2015

entity framework generate from database

  • First, open server explorer, add connection to the table you want to work on. 

    Second, generate class model.
  • Project -> Add New Item…
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter BloggingModel as the name and click OK
  • This launches the Entity Data Model Wizard
  • Select Generate from Database and click Next

3. Reverse Engineer Model

We’re going to make use of Entity Framework Designer, which is included as part of Visual Studio, to create our model.
  • Project -> Add New Item…
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter BloggingModel as the name and click OK
  • This launches the Entity Data Model Wizard
  • Select Generate from Database and click Next
    Generate from Database selected in first step of wizard
  • Select the connection to the database you created in the first section, enter BloggingContext as the name of the connection string and click Next
    DatabaseFirst.Blogging database selected and BloggingContext entered as connection string name in second step of wizard
  • Click the checkbox next to ‘Tables’ to import all tables and click ‘Finish’
    All tables selected for import in final step of wizard

Once the reverse engineer process completes the new model is added to your project and opened up for you to view in the Entity Framework Designer. An App.config file has also been added to your project with the connection details for the database.
EF Designer showing model with Blog and Post entity









How to manually update your Entity Framework model classes when they get out of sync with your database


The ADO.NET Entity Framework is an awesome way to quickly pull data into an app.  I’ve been building apps for a really long time and have used all the frameworks that have come out over the years.  IMO EF is the best one I’ve used yet.
Well, I should say it is awesome MOST of the time.  The biggest beef I have with it is that the models can get out of sync with the database and saving the EDMX file and running the “Update Model from Database…” feature doesn’t work.
image
(The Update Model from Database option doesn’t update the object model and class files)
For example, let’s say I start with this model:
image
And I add a new table to the DB called Foo:
image
I then go back to my app and add my new table to the model using the “Update Model from Database…” feature:
image
And it is added to the diagram:
image
But the corresponding object model and CS file wasn’t generated:
image
Up until today I would have recreated the EDMX file, which is a big pain.  But I discovered that there’s a context menu item called “Run Custom Tool”.
The most obvious choice is to select “Run Custom Tool” from the EDMX file, but that doesn’t do anything:
image
If you F4 the corresponding T4 file you can see that the custom tool associated with the T4 file is: TextTemplatingFileGenerator
image

So, I selected “Run Custom Tool” from the T4 context menu:
image
And that generated the Foo.cs file:
image
But I still didn’t have the Foo class in my object model:
image
So I selected the “Run Custom Tool” on the “ConfSpeakers.Context.tt” file:
image
And that generated the correct object model:
image
So anytime you find your EF model and/or class files out of sync with your database then you need to manually run the “Run Custom Tool” command on both the [Entities].tt file and the [Entities].Context.tt file.

This is a big pain.  I will ping the product team to see if they can run the custom tool on the EDMX file when it is saved and recursively run on all the tt files associated with that EDMX parent.

No comments: