'How to get DBcontext.ChangeTracker.Entries(of ???)) for a class known only on runtime

I'm using entity framework I'm trying to make a general function that works for an object for which the class is know only on runtime. I have an instruction that use an expression similar to this :

context.ChangeTracker.Entries(of Myobj1)

Of course that if I know the class name "MyObj1" the above expression is working correctly.

But, at runtime , I only have an variable : Dim tp as type , which contain the class , and i need an expression like this :

context.ChangeTracker.Entries(of tp)

Thank you !



Solution 1:[1]

Assumes that obj is an instance of MyObj1, then you can do as:

DBcontext.ChangeTracker.Entries().Where(x => x.GetType() == obj.GetType())

Solution 2:[2]

you can use this code to only commit changes on specific class:

        public static void ResetDBContext(this DBcontext context, Type type )
        {
            var entries = context.ChangeTracker
                                 .Entries()
                                 .Where(e => e.State != EntityState.Unchanged && e.Entity.GetType()==type)
                                 .ToArray();

            foreach (var entry in entries)
            {
                switch (entry.State)
                {
                    case EntityState.Modified:
                        entry.State = EntityState.Unchanged;
                        break;
                    case EntityState.Added:
                        entry.State = EntityState.Detached;
                        break;
                    case EntityState.Deleted:
                        entry.Reload();
                        break;
                }
            }
        }

then pass your type to this function (with obj.GetType())

Solution 3:[3]

public interface IFollow
{
   //Signature interface
}

public class EmailOption : IFollow, IBaseEntity
{
    public int Id { get; set; }
    public string SmtpServer { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public int Port { get; set; }
    public bool EnableSsl { get; set; }
}
var entries = _context.ChangeTracker.Entries()
    .Where(x => x.Entity is IFollow 
    && (x.State == EntityState.Added 
    || x.State == EntityState.Modified 
    || x.State == EntityState.Deleted)).ToList();

Solution 4:[4]

You can use System.Linq to make your life easier

If you have an instance of the object:

DBcontext.ChangeTracker.Entries().Where(x => x == myObject)

If you only know the type during run-time

DBcontext.ChangeTracker.Entries().Where(x => x.GetType() == typeof(MyObj1))

Else, if you want to check whether it 'is' a class:

DBcontext.ChangeTracker.Entries().Where(x => x is MyObj1)

If you're dealing with a single object, you may use First/FirstOrDefault/Single/SingleOrDefault based on your preference and logic

Solution 5:[5]

DBcontext.ChangeTracker.Entries().Where(x => x.Entity.GetType() == typeof(MyObj1))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vxchin
Solution 2 Hamed Damirchi
Solution 3
Solution 4
Solution 5 FelixSFD