Note that if change tracking is disabled in your context, an ObjectStateManager or ChangeTracker may return that the object is not in the ObjectContext , even if it is actually already there. Therefore, if you try to attach such an object, it will throw an exception.
context.Set<T>.Local.Any(e => e.Id == id);
the event works if change tracking is disabled.
if you donβt know the type of the object, there is a different approach, either you define a method using reflection, or other methods like this int GetIdOf(object entity){...}
Or you define the interface used by your classes, e.g.
public interface IMyEntity { int Id{get;set;} }
and use it as follows:
context.Set(e.GetType()).Local.Cast<IMyEntity>().Any(e => e.Id == id);
Dubbs777 Apr 07 '14 at 13:40 2014-04-07 13:40
source share