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.
Version 4.1 of the Entity Framework contains both the Code First approach and the new DbContext API. This API provides a more productive surface for working with the Entity Framework and can be used with the Code First, Database First, and Model First approaches. This is the first post of a twelve part series containing collections of patterns and code fragments showing how features of the new API can be used.
The posts in this series do not contain complete walkthroughs. If you haven’t used EF 4.1 before then you should read Code First Walkthrough or Model and Database First with DbContext before tackling this post.
Many of the code fragments in the posts of this series make use of the following Code First model. Assume that the context and entity types used in the fragments are from this model unless they are explicitly defined differently in the post.
The code below can be pasted into Program.cs of a standard C# console application and many of the code fragments can then be tested by inserting them into the Main method.
The model below contains four entity types: Princess, Unicorn, Castle, and LadyInWaiting. A princess can have many unicorns and each unicorn is owned by exactly one princess. A princess has exactly one lady-in-waiting in each castle.
A castle has a location represented by the complex type Location, which in turn has a nested complex type ImaginaryWorld
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Metadata.Edm; using System.Data.Objects; using System.Linq; namespace Magic.Unicorn { public class Princess : IPerson { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Unicorn> Unicorns { get; set; } public virtual ICollection<LadyInWaiting> LadiesInWaiting { get; set; } } public class Unicorn { public int Id { get; set; } public string Name { get; set; } [Timestamp] public byte[] Version { get; set; } public int PrincessId { get; set; } // FK for Princess reference public virtual Princess Princess { get; set; } } public class Castle { [Key] public string Name { get; set; } public Location Location { get; set; } public virtual ICollection<LadyInWaiting> LadiesInWaiting { get; set; } } [ComplexType] public class Location { public string City { get; set; } public string Kingdom { get; set; } public ImaginaryWorld ImaginaryWorld { get; set; } } [ComplexType] public class ImaginaryWorld { public string Name { get; set; } public string Creator { get; set; } } public class LadyInWaiting : IPerson { [Key, Column(Order = 0)] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int PrincessId { get; set; } // FK for Princess reference [Key, Column(Order = 1)] public string CastleName { get; set; } // FK for Castle reference public string FirstName { get; set; } public string Title { get; set; } [NotMapped] public string Name { get { return String.Format("{0} {1}", Title, FirstName); } } public virtual Castle Castle { get; set; } public virtual Princess Princess { get; set; } } public interface IPerson { string Name { get; } } public class UnicornsContext : DbContext { public DbSet<Unicorn> Unicorns { get; set; } public DbSet<Princess> Princesses { get; set; } public DbSet<LadyInWaiting> LadiesInWaiting { get; set; } public DbSet<Castle> Castles { get; set; } } public class UnicornsContextInitializer : DropCreateDatabaseAlways<UnicornsContext> { protected override void Seed(UnicornsContext context) { var cinderella = new Princess { Name = "Cinderella" }; var sleepingBeauty = new Princess { Name = "Sleeping Beauty" }; var snowWhite = new Princess { Name = "Snow White" }; new List<Unicorn> { new Unicorn { Name = "Binky" , Princess = cinderella }, new Unicorn { Name = "Silly" , Princess = cinderella }, new Unicorn { Name = "Beepy" , Princess = sleepingBeauty }, new Unicorn { Name = "Creepy" , Princess = snowWhite } }.ForEach(u => context.Unicorns.Add(u)); var efCastle = new Castle { Name = "The EF Castle", Location = new Location { City = "Redmond", Kingdom = "Rainier", ImaginaryWorld = new ImaginaryWorld { Name = "Magic Unicorn World", Creator = "ADO.NET" } }, }; new List<LadyInWaiting> { new LadyInWaiting { Princess = cinderella, Castle = efCastle, FirstName = "Lettice", Title = "Countess" }, new LadyInWaiting { Princess = sleepingBeauty, Castle = efCastle, FirstName = "Ulrika", Title = "Lady" }, new LadyInWaiting { Princess = snowWhite, Castle = efCastle, FirstName = "Yolande", Title = "Duchess" } }.ForEach(l => context.LadiesInWaiting.Add(l)); } } public class Program { public static void Main(string[] args) { Database.SetInitializer(new UnicornsContextInitializer()); // Many of the code fragments can be run by inserting them here } } }
In this part of the series we introduced the topics that will be covered in upcoming parts and also defined a Code First model to work with.
As always we would love to hear any feedback you have by commenting on this blog post.
For support please use the Entity Framework Forum.
Arthur Vickers Developer ADO.NET Entity Framework