Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 695
Keith Rousseau Avatar asked Jan 25 '26 11:01

Keith Rousseau


1 Answers

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.

like image 52
Keith Rousseau Avatar answered Jan 27 '26 23:01

Keith Rousseau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!