The release described in this post is no longer current.
Entity Framework 4.1 RTW is now available.
I’ve been hearing rumors Entity Framework (EF) is more robust now. I did a few searches and found the latest version EF CTP2. I was happy to find EF has a “code only” method for generating the database schema and a good take on configuration. If you are intrested in the code only method check out this walk through. I went through it and built my own application to get a handle on this new feature. Here were the steps I took to get up and running. You will need Visual Studio 2010, SQL, and CTP2 version of EF. 1) Install CTP2 Entity Framework Preview 2
public class Profile
{
public Profile() { }
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string PermaUrl { get; set; }
public DateTime Created { get; set; }
public DateTime? LastSeen { get; set; }
}
public class User
{
public User() { }
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Password {get; set;}
public ICollection<profile> Profiles { get; set; }
}
3) Next create a new class project in your solution for your EF models. I called mine UserManager.Model. I think it is good to make a seperate project as this is going to setup all your class context for connecting to EF. You will also need to add a project reference to the project that has your objects which you want to be mapped in the database and a reference to Microsoft.Data.Entity.CTP and System.Data.Entity.
public class UserModel : ObjectContext
{
public UserModel(EntityConnection connection)
: base(connection)
{
DefaultContainerName = "UserModel";
}
public IObjectSet User
{
get { return base.CreateObjectSet (); }
}
public IObjectSet Profiles
{
get { return base.CreateObjectSet (); }
}
}
4) Create a new console application to automatically generate your database. I called mine “UserManager.GenerateDatabase”. This is the application that will use the object context to autogenerate the database. You can also do specific table configuration and other initialization. You can use a RegisterConfigurations method. The walk through above goes into more detail on that.
var builder = new ContextBuilderMy project and auto generated tables ended up looking like this:(); var connStr = ConfigurationManager.ConnectionStrings["UserDBConn"].ConnectionString; var connection = new SqlConnection(connStr); using (var ctx = builder.Create(connection)) { if (ctx.DatabaseExists()) ctx.DeleteDatabase(); ctx.CreateDatabase();