EF Feature CTP5: Model & Database First with DbContext
The information in this post is out of date.
Visit msdn.com/data/ef for the latest information on current and past releases of EF.
For Model First see https://msdn.com/data/jj205424
For Database First see https://msdn.com/data/jj206878
We have released Entity Framework Feature Community Technology Preview 5 (CTP5) . Feature CTP5 contains a preview of new features that we are planning to release as a stand-alone package in Q1 of 2011 and would like to get your feedback on. Feature CTP5 builds on top of the existing Entity Framework 4 (EF4) functionality that shipped with .NET Framework 4.0 and Visual Studio 2010 and is an evolution of our previous CTPs.
This post will provide an introduction to Model First and Database First development using the Entity Data Model Designer in Visual Studio.
1. Install EF CTP5
If you haven’t already done so then you need to install Entity Framework Feature CTP5.
2. Create the Application
To keep things simple we’re going to build up a basic console application that uses the DbContext to perform data access:
- Open Visual Studio 2010
- File -> New -> Project…
- Select “Windows” from the left menu and “Console Application”
- Enter “ModelFirstSample” as the name
- Select “OK”
3. Create the Model
Let’s go ahead and add an Entity Data Model to our project;
- Project –> Add New Item…
- Select ‘Data’ from the left menu
- Select ‘ADO.NET Entity Data Model’ from the list of available items
- Name the model ‘PersonModel.edmx’
- Click ‘Add’
We are going to use Model First for this walkthrough but if you are mapping to an existing database you would now select ‘Generate from database’, follow the prompts and then skip to step 4.
- Select ‘Empty model’
- Click ‘Finish’
Let’s add a Person entity to our model:
- On the design surface; Right Click –> Add –> Entity
- Name the entity ‘Person’
- Click ‘OK’
- On the Person entity; Right Click –> Add –> Scalar Property
- Name the property ‘Full Name’
Now that we’ve defined the model we can generate a database schema to store our data:
- On the design surface; Right Click –> Generate Database from Model
- Click ‘New Connection…’
- Specify the details of the database you wish to create
- Click ‘OK’
- If prompted to create the database; click ‘Yes’
- Click ‘Next’ then ‘Finish’
- On the generated script; Right Click –> Execute SQL…
- Speccify your database server and click ‘Connect’
4. Swap to DbContext Code Generation
The PersonModel is currently generating a derived ObjectContext and entity classes that derive from EntityObject, we want to make use of the simplified DbContext API:
- On the design surface; Right Click –> Add Code Generation Item…
- Select ‘Code’ from the left menu
- Select ‘ADO.NET DbContext Generator
- Name the item ‘PersonModel.tt’
- Click ‘Add’
You’ll notice that two items are added to your project:
- PersonModel.tt
This template generates very simple POCO classes for each entity in your model - PersonModel.Context.tt
This template generates a derived DbContext to use for querying and persisting data
5. Read & Write Data
Time to access some data, I’m padding out the Main method in Program.cs file as follows;
class Program
{
static void Main(string[] args)
{
using (var db = new PersonModelContainer())
{
// Save some data
db.People.Add(new Person { FullName = "Bob" });
db.People.Add(new Person { FullName = "Ted" });
db.People.Add(new Person { FullName = "Jane" });
db.SaveChanges();
// Use LINQ to access data
var people = from p in db.People
orderby p.FullName
select p;
Console.WriteLine("All People:");
foreach (var person in people)
{
Console.WriteLine("- {0}", person.FullName);
}
// Change someones name
db.People.First().FullName = "Janet";
db.SaveChanges();
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
Summary
In this walkthrough we looked at Model First development using EF Feature CTP5. We looked at building a model, generating a database, swapping to DbContext code generation and then saving and querying data.
Feedback & Support
As always we would love to hear any feedback you have by commenting on this blog post.
For support please use the Entity Framework Pre-Release Forum.
Rowan Miller
Program Manager
ADO.NET Entity Framework
Comments
Anonymous
December 07, 2010
Go tutorial step by step, but instead db.People get generated db.PersonSet. What I did wrong?Anonymous
December 07, 2010
@andrex Are you sure you are not targeting CTP4? The "Set" naming pattern was removed in CTP5. If you do not have EntityFramework.dll in your project you've got the wrong reference.Anonymous
December 28, 2010
I've the same problem of andrex. I've referenced the EntityFramework.dll too.I checked and the CTP4 is uninstalled. What I do wrong ? Thanks!Anonymous
December 28, 2010
I've the same problem of andrex. I've referenced the EntityFramework.dll too.I checked and the CTP4 is uninstalled. What I did wrong ? Thanks!Anonymous
December 30, 2010
I've the same problem of andrex and stuzzo.Anonymous
January 02, 2011
@andrex, @mrxliu Hi, I opened a thread in the Entity Framework Pre-Release Forum, here's the discussion social.msdn.microsoft.com/.../74f656c2-db4e-4376-af64-e78e7af26c13.Anonymous
January 07, 2011
If you got PersonSet instead of People, looks like you have to change property "Pluralize New Objects" to true in you PersonModel.edmxAnonymous
February 05, 2011
Just wondering. If I already have a database and regular EF4 installed, and have created POCO classes etc: is dbset/dbcontext going to give me anything that objectset/objectcontext isn't giving me?Anonymous
February 11, 2011
Can anyone answer the question in this thread? social.msdn.microsoft.com/.../86c02ec1-fd3d-4747-93cb-c41cb332db4c