码迷,mamicode.com
首页 > 其他好文 > 详细

entityframework单例模式泛型用法

时间:2015-04-07 19:10:04      阅读:113      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!