entityframework单例模式泛型用法
public class yms_Entity<T> where T :DbContext
{
private static T _instance;
public static readonly object SyncObject = new object();
public static T GetEntity()
{
if (_instance == null)
{
lock (SyncObject)
{
if (_instance == null)
{
_instance = (T)Activator.CreateInstance(typeof(T), true);
}
}
}
return _instance;
}
public static int SaveChanges<Y>(Y y) where Y : class
{
var entity = _instance as DbContext;
DbEntityEntry<Y> rmEntry = entity.Entry(y);
if (rmEntry.State != EntityState.Modified)
{
rmEntry.State = EntityState.Modified;
}
else
{
entity.Set<Y>().Attach(y);
}
return entity.SaveChanges();
}
public static void SetInstance(T value)
{
_instance = value;
}
}