The official source of information on Managed Providers, DataSet & Entity Framework from Microsoft
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 http://msdn.com/data/jj205424
For Database First see http://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.
If you haven’t already done so then you need to install Entity Framework Feature CTP5.
To keep things simple we’re going to build up a basic console application that uses the DbContext to perform data access:
Let’s go ahead and add an Entity Data Model to our project;
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.
Let’s add a Person entity to our model:
Now that we’ve defined the model we can generate a database schema to store our data:
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:
You’ll notice that two items are added to your project:
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(); } }
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.
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