views:

459

answers:

3

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?

+2  A: 

I believe ConntextBuilder was replaced with ModelBuilder in the latest CTP. The new builder does not function identically to its predecessor, so you might want to read up on it here:

http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4codefirstwalkthrough.aspx

jrista
+2  A: 

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.

Keith Rousseau
Awesome! just what I needed it! Thanks :)
tricat
+2  A: 

The ContextBuilder from CTP3 would also automatically discover the entity types that you exposed in ObjectSet properties, you can still do this by calling the ModelBuilder.DiscoverEntitiesFromContext method.

var builder = new ModelBuilder();
builder.DiscoverEntitiesFromContext(typeof(MyContext));
var model = builder.CreateModel();
var context = model.CreateObjectContext<MyContext>(connection);

Obviously this split adds an extra step into the context creation process, the motivation behind it is that DbModel (or a type similar to it) will become the primary representation for models in EF in the future and will be the output of other model creation options beyond ModelBuilder. DbModel is also the unit that should be cached throughout your application.

Rowan Miller
The point is that I don't want to create a custom MyContext class. I am injecting a generic ObjectContext into a Repository and creating my ObjectSet<T> inside of the Repository<T>.
Keith Rousseau