Exemplo n.º 1
0
 public static T Query <T>(Expression <Func <T, bool> > express) where T : class
 {
     using (AaronContext context = new AaronContext())
     {
         return(context.Set <T>().FirstOrDefault(express));
     }
 }
Exemplo n.º 2
0
 public static IEnumerable <T> QueryList <T>(Expression <Func <T, bool> > express)
     where T : class
 {
     using (AaronContext context = new AaronContext())
     {
         return(context.Set <T>().Where(express).ToList());
     }
 }
Exemplo n.º 3
0
 public static void AddList <T>(List <T> list)
     where T : class
 {
     using (AaronContext context = new AaronContext())
     {
         context.Set <T>().AddRange(list);
         context.SaveChanges();
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 添加
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="model"></param>
 public static void Add <T>(T model)
     where T : class
 {
     using (AaronContext context = new AaronContext())
     {
         context.Set <T>().Add(model);
         context.SaveChanges();
     }
 }
Exemplo n.º 5
0
 public static void Delete <T>(T model)
     where T : class
 {
     //用循环,然后最后再SaveChange就可以了
     using (AaronContext context = new AaronContext())
     {
         context.Set <T>().Add(model);
         context.Entry <T>(model).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 修改某一模型数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="model"></param>
 public static void Update <T>(T model) where T : class
 {
     using (AaronContext context = new AaronContext())
     {
         if (context.Entry <T>(model).State == EntityState.Detached)
         {
             context.Set <T>().Attach(model);
             context.Entry <T>(model).State = EntityState.Modified;
         }
         context.SaveChanges();
     }
 }