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
2) Create a new class library project in your solution. This will be for your objects that will get mapped to the database. I named mine “UserSchema”. In this project you are just creating normal classes without any references to EF. Below are the two classes I used for testing. Notice they have no meta tags or any other tags that would be EF specific.
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 ContextBuilder(); var connStr = ConfigurationManager.ConnectionStrings["UserDBConn"].ConnectionString; var connection = new SqlConnection(connStr); using (var ctx = builder.Create(connection)) { if (ctx.DatabaseExists()) ctx.DeleteDatabase(); ctx.CreateDatabase();
My project and auto generated tables ended up looking like this:

So after going through this quick setup I got a taste of what is to come. I’m still going to stick with ActiveRecord for my ORM but I think EF will definitly be an option in the future. Also, the code only mode will be a little more comfortable for people who have an ActiveRecord background.
update – Code Only Looking Good (part 2)
update CTP3 – Code First Castle Windsor
Entity Framework blog posts
