首页 > 其他 > 详细

entityframework单例模式泛型用法

时间:2015-04-07 19:09:24      阅读:419      评论:0      收藏:0      [点我收藏+]

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;
}

}

entityframework单例模式泛型用法

原文:http://www.cnblogs.com/jacle169/p/4398924.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!