Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Merge of DbContext: how to check if entity is all ready attached?

Given the following code:

    void MergeDbContext(DbContext aSourceDbContext, DbContext aDestinationDbContext)
    {
        var sourceEntities = aSourceDbContext.ChangeTracker.Entries().ToList();
        foreach (DbEntityEntry entry in sourceEntities)
        {
            object entity = entry.Entity;
            entry.State = EntityState.Detached;

            // check if entity is all ready in aDestinationDbContext if not attach
            bool isAttached = false;// TODO I don't know how to check if it is all ready attched.
            if (!isAttached)
            {
                aDestinationDbContext.Set(entity.GetType()).Attach(entity);
            }
        }
    }

How can I generically determine if an entity exists in the context.

like image 557
Steven T. Cramer Avatar asked Jun 01 '15 15:06

Steven T. Cramer


People also ask

Does DbContext need to be disposed?

Don't dispose DbContext objects. Although the DbContext implements IDisposable , you shouldn't manually dispose it, nor should you wrap it in a using statement. DbContext manages its own lifetime; when your data access request is completed, DbContext will automatically close the database connection for you.

Is entity Framework DbContext thread safe?

DbContext is not thread-safe. Do not share contexts between threads. Make sure to await all async calls before continuing to use the context instance. An InvalidOperationException thrown by EF Core code can put the context into an unrecoverable state.

What is DbContext in Entity Framework Core?

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. Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.

How do you call a view in Entity Framework?

Step 1 − Create a new Console Application project. Step 2 − Right-click on project in solution explorer and select Add → New Item. Step 3 − Select ADO.NET Entity Data Model from the middle pane and enter name ViewModel in the Name field. Step 4 − Click Add button which will launch the Entity Data Model Wizard dialog.


1 Answers

This is an extension method that looks for an object in a context's state manager by the following steps:

  • Get the type's key properties
  • Get the object's key values
  • Check if there is any entity in the context having the object's type and key values.
public static bool IsAttached<T>(this DbContext context, T entity)
    where T : class
{
    if (entity == null) return false;

    var oc = ((IObjectContextAdapter)context).ObjectContext;

    var type = ObjectContext.GetObjectType(entity.GetType());

    // Get key PropertyInfos
    var propertyInfos = oc.MetadataWorkspace
          .GetItems(DataSpace.CSpace).OfType<EntityType>()
          .Where(i => i.Name == type.Name)
          .SelectMany(i => i.KeyProperties)
          .Join(type.GetProperties(), ep => ep.Name, pi => pi.Name, (ep,pi) => pi);

    // Get key values
    var keyValues = propertyInfos.Select(pi => pi.GetValue(entity)).ToArray();

    // States to look for    
    var states = System.Data.Entity.EntityState.Added|System.Data.Entity.EntityState.Modified
                |System.Data.Entity.EntityState.Unchanged|System.Data.Entity.EntityState.Deleted;

     // Check if there is an entity having these key values
    return oc.ObjectStateManager.GetObjectStateEntries(states)
             .Select(ose => ose.Entity).OfType<T>()
             .Any(t => propertyInfos.Select(i => i.GetValue(t))
                                    .SequenceEqual(keyValues));
}

Usage:

bool isAttached = dbContext.IsAttached(entity);
like image 182
Gert Arnold Avatar answered Sep 27 '22 22:09

Gert Arnold