110,549
社区成员
发帖
与我相关
我的任务
分享
public static List<T> GetEntiyToList<T>(string Filter = "") where T : class
{
using (var ctx = new ERPContext())
{
return ctx.Set<T>().ToList();
}
}
public static void SaveEntity<T>(T entity) where T : class
{
var state = ctx.Entry(entity).State;
if (state == EntityState.Unchanged)
ctx.Entry(entity).State = EntityState.Modified;
else if (state == EntityState.Detached)
ctx.Set<T>().Add(entity);
}
public static void SaveEntity<T>(T entity) where T : class
{
using (var ctx = new ERPContext())
{
object[] keyvalue = GetKeyValues(entity);
var ExistsEntity = ctx.Set<T>().Find(keyvalue);
if (ExistsEntity == null)
{
ctx.Set<T>().Add(entity);
}
else
{
var props = ExistsEntity.GetType().GetProperties();
foreach (var p in props)
{
foreach (var p1 in props)
{
if (p1.Name == p.Name && p1.GetValue(entity) != p.GetValue(ExistsEntity))
p.SetValue(ExistsEntity, p1.GetValue(entity));
}
}
}
ctx.SaveChanges();
}
}
public static void DeleteEntity<T>(T entity) where T : class
{
using (var ctx = new ERPContext())
{
ctx.Set<T>().Remove(entity);
ctx.SaveChanges();
}
}