Model-First in the Entity Framework

So I finally had the situation come up to do a model-first approach to Entity Framework (EF).  It was shockingly simple.  Normally I design my databases first and go from there, this time I did my EF model.  Now this was just a simple four entity deal but I thought it would be a great Hello Model-First.

You simply create your entities in the designer, adding properties and adjusting their properties.  You know, like indicating someNum is an int32 instead of the default string, if something can be null, default values and so on.  You also add your associations.  Then you generate database from model.  I did this by right clicking the design surface and clicking on Generate Database from Model…   This will create a SQL script that you can run against your database.  In my case I did create an empty database with a SQL user before hand that I used in my connection information for generating the database.

If you still think it is harder than that, you can read this great article by Microsoft.  I wanted to look it up and make sure I did not leave something out and found that article.  I still suggest you look over the article.  It goes into much more detail than my short text, and they include screen shots.

One thing I did go back and do was change the name of that connection.  Visual Studio named it MyPartModelObjectContainer (or something close to that), where MyPart was basically what I named the Model.  This was simple to do as well.  You rename it int eh connectionString area of your web.config and in the properties of the model.edmx file.  You can see its properties by just clicking on the design surface, not an entity or anything, just on the empty space.

Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on August 30, 2011, in Entity Framework. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: