Search code examples
c#.netentity-frameworkormentity-framework-4

Building an ObjectContext in EF Code First CTP4


I just upgraded to EF Code First CTP 4 and it looks like the ContextBuilder class was removed. I currently create my ObjectContext like so (in CTP 3):

var contextBuilder = new ContextBuilder<ObjectContext>();
var connstr = ConfigurationManager.ConnectionStrings["MyConn"];
var connection = new SqlConnection(connstr.ConnectionString);
var ctx = contextBuilder.Create(connection);

I do not want to create a hardcoded class deriving from ObjectContext like so many of their examples seem to do. Anyone know how to do this in the new version?


Solution

  • Here is the modified way that it should be done:

    var modelBuilder = new ModelBuilder();
    var dbModel = modelBuilder.CreateModel();
    var ctx = dbModel.CreateObjectContext<ObjectContext>(connection);
    

    Note that you only want to call CreateModel once per application.