EntityConnectionFactory.CreatePersistent

Oct 2, 2012 at 9:09 AM
Edited Nov 16, 2012 at 10:00 AM

Hello,


I've seen that there is a CreatePersistent function in the EntityConnectionFactory class. Does this mean that the changes to the DbContext can be persisted?

 

Best regards, 

Botond

Coordinator
Oct 2, 2012 at 10:04 AM
Edited Oct 2, 2012 at 10:14 AM

The CreatePersistent methods of the factory classes (ObjectContextFactory, EntityConnectionFactory, DbConnectionFactory) instantiate data endpoints that communicate with an in-memory database instance that can be accessed later, it is not deleted if the endpoint is dismissed.

The underlying in-memory database instance is identified by a string if it is created by the DbConnectionFactory, or by an entity connection string if it is created by the EntityConnectionFactory or ObjectContextFactory.

For example:

EntityConnection entityConnection1 = 
EntityConnectionFactory.CreatePersistent("name=NorthwindObjectContext"); using (var ctx = new NorthwindObjectContext(entityConnection1)) { // Do changes } EntityConnection entityConnection2 =
EntityConnectionFactory.CreatePersistent("name=NorthwindObjectContext"); using (var ctx = new NorthwindObjectContext(entityConnection2)) { // Query changes }

In the second "object context block" the query will show the changes you have made in the first one, because the entity connection string is same and references to the sample instance of in-memory database.

By the way, I am going to create a blog post about the differences between CreatePersistent and CreateTransient in the near future.