Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DbContext from Entity in Entity Framework

I'm deep somewhere in the Business layer in a debugging session in Visual Studio trying to figure out why an Entity is behaving strangely when trying to persist the changes.

It would really be helpful to get a reference to the DbContext this Entity belongs to, at this point in the call stack.

I.e. to see what the state is of this Entity is (Unchanged, Modified, etc).

So I'm looking for a helper method like this:

var db_context = DbContextHelpers.GetDbContext(entity);  // after that I could do something like this var state = db_context.Entry(entity); 

I can use this stuff in the Immediate window during debugging.

Anyone any suggestions?

Extra notes

The Entity must be aware of the DbContext somewhere, because it is using it for lazy loading navigation properties?

like image 587
Dirk Boer Avatar asked Aug 10 '14 15:08

Dirk Boer


People also ask

Is DbContext part of Entity Framework?

The DbContext class is an integral part of Entity Framework. An instance of DbContext represents a session with the database which can be used to query and save instances of your entities to a database. DbContext is a combination of the Unit Of Work and Repository patterns.

How does DbContext work in Entity Framework?

As per Microsoft “A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.” In simplified way we can say that DbContext is the bridge between Entity Framework and Database.

What is difference between DbContext and ObjectContext?

Definition. DBContext is a wrapper of ObjectContext that exposes the most commonly used features of ObjectContext. In contrast, Object Context is a class of the core Entity framework API that allows performing queries and tracking the updates made to a database using strongly typed entity classes.


Video Answer


2 Answers

public static DbContext GetDbContextFromEntity(object entity) {     var object_context = GetObjectContextFromEntity( entity );      if ( object_context == null )         return null;      return new DbContext( object_context, dbContextOwnsObjectContext: false ); }  private static ObjectContext GetObjectContextFromEntity(object entity) {     var field = entity.GetType().GetField("_entityWrapper");      if ( field == null )         return null;      var wrapper  = field.GetValue(entity);     var property = wrapper.GetType().GetProperty("Context");     var context  = (ObjectContext)property.GetValue(wrapper, null);      return context; } 
like image 170
Dirk Boer Avatar answered Sep 29 '22 17:09

Dirk Boer


For EF6, I modified Dirk's answer slightly:

    public static DbContext GetDbContextFromEntity(object entity)     {         var object_context = GetObjectContextFromEntity(entity);          if (object_context == null || object_context.TransactionHandler == null)             return null;          return  object_context.TransactionHandler.DbContext;     }      private static ObjectContext GetObjectContextFromEntity(object entity)     {         var field = entity.GetType().GetField("_entityWrapper");          if (field == null)             return null;          var wrapper = field.GetValue(entity);         var property = wrapper.GetType().GetProperty("Context");         var context = (ObjectContext)property.GetValue(wrapper, null);          return context;     } 

No new DbContext() and it's castable into your main Entities class.

Note: To the above question of null return value, this will happen if the entity has not been saved/committed. New entities that can only be found in .Local do not seem to have the "_entityWrapper" field.

like image 43
kernelk Avatar answered Sep 29 '22 17:09

kernelk