This video and step-by-step walkthrough provide an introduction to Model First development using Entity Framework. Model First allows you to create a new model using the Entity Framework Designer and then generate a database schema from the model. The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that you interact with in your application are automatically generated from the EDMX file.
( more video options - including download)
You will need to have Visual Studio 2010 or Visual Studio 2012 installed to complete this walkthrough.
If you are using Visual Studio 2010, you will also need to have NuGet installed.
To keep things simple we’re going to build a basic console application that uses the Model First to perform data access:
We’re going to make use of Entity Framework Designer, which is included as part of Visual Studio, to create our model.
.png)
The Entity Framework Designer is opened with a blank model. Now we can start adding entities, properties and associations to the model.
.png)
Now that we have a couple of entities, it’s time to add an association (or relationship) between them.
.png)
We now have a simple model that we can generate a database from and use to read and write data..png)
If you are working in Visual Studio 2010 there are some additional steps you need to follow to upgrade to the latest version of Entity Framework. Upgrading is important because it gives you access to an improved API surface, that is much easier to use, as well as the latest bug fixes.
First up, we need to get the latest version of Entity Framework from NuGet.
Next, we need to swap our model to generate code that makes use of the DbContext API, which was introduced in later versions of Entity Framework.
.png)
Given our model, Entity Framework can calculate a database schema that will allow us to store and retrieve data using the model.
The database server that is installed with Visual Studio is different depending on the version of Visual Studio you have installed:
Let's go ahead and generate the database.
.png)
.png)
Now that we have a model it’s time to use it to access some data. The classes we are going to use to access data are being automatically generated for you based on the EDMX file.
This screen shot is from Visual Studio 2012, if you are using Visual Studio 2010 the BloggingModel.tt and BloggingModel.Context.tt files will be directly under your project rather than nested under the EDMX file.
Implement the Main method in Program.cs as shown below. This code creates a new instance of our context and then uses it to insert a new Blog. Then it uses a LINQ query to retrieve all Blogs from the database ordered alphabetically by Title.
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
You can now run the application and test it out.
| Enter a name for a new Blog: ADO.NET Blog All blogs in the database: ADO.NET Blog Press any key to exit... |
Now it’s time to make some changes to our model, when we make these changes we also need to update the database schema.
We’ll start by adding a new User entity to our model.
.png)
We now have an updated model and we are ready to update the database to accommodate our new User entity type.
In this walkthrough we looked at Model First development, which allowed us to create a model in the EF Designer and then generate a database from that model. We then used the model to read and write some data from the database. Finally, we updated the model and then recreated the database schema to match the model.