zoukankan      html  css  js  c++  java
  • Entity Framework公共的增删改方法

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    namespace My
    {
        /// <summary> Entity Framework公共的增删改方法。返回的是受影响的行数 </summary>
        public class PublicStore
        {
           //新增
           public static int InsertObject(object obj)
            {
                Type t = obj.GetType();
                int effect = -1;
                using (MyContext con = new MyContext())
                {
                    DbSet set = con.Set(t);
                    set.Add(obj);
                    effect = con.SaveChanges();
                    return effect;
                }
            }
            
            //批量新增
            public static int InsertObjects(IEnumerable<object> objs)
            {
                int effect = 0;
    
                var et = objs.GetEnumerator();
                if (et.MoveNext())
                {
                    Type t = et.Current.GetType();
                    using (MyContext con = new MyContext())
                    {
                        DbSet set = con.Set(t);
                        foreach (var o in objs)
                        {
                            set.Add(o);
                        }
                        effect = con.SaveChanges();
                    }
                }
    
                return effect;
            }
            
            //修改
            public static int ModifyObject(object obj)
            {
                int effect = -1;
                using (MyContext con = new MyContext())
                {
                    DbEntityEntry entry = con.Entry(obj);
                    entry.State = System.Data.EntityState.Modified;
                    effect = con.SaveChanges();
                    return effect;
                }
            }
    
            //批量修改
            public static int ModifyObjects(IEnumerable<object> objs)
            {
                int effect = 0;
                var et = objs.GetEnumerator();
                if (et.MoveNext())
                {
                    Type t = et.Current.GetType();
                    using (MyContext con = new MyContext())
                    {
                        foreach (var o in objs)
                        {
                            DbEntityEntry entry = con.Entry(o);
                            entry.State = System.Data.EntityState.Modified;
                        }
                        effect = con.SaveChanges();
                    }
                }
    
                return effect;
            }
            
            //删除
            public static int DeleteObject(object obj)
            {
                int effect = -1;
                using (MyContext con = new MyContext())
                {
                    DbEntityEntry entry = con.Entry(obj);
                    entry.State = System.Data.EntityState.Deleted;
                    effect = con.SaveChanges();
                    return effect;
                }
            }
            
            //批量删除
            public static int DeleteObjects(IEnumerable<object> objs)
            {
                int effect = 0;
    
                var et = objs.GetEnumerator();
                if (et.MoveNext())
                {
                    Type t = et.Current.GetType();
                    using (MyContext con = new MyContext())
                    {
                        foreach (var o in objs)
                        {
                            DbEntityEntry entry = con.Entry(o);
                            entry.State = System.Data.EntityState.Deleted;
                        }
                        effect = con.SaveChanges();
                    }
                }
                return effect;
            }
        }
    }
  • 相关阅读:
    第03组 Beta冲刺(2/5)
    2019 SDN上机第6次作业
    第03组 Beta冲刺(1/5)
    2019 SDN上机第5次作业
    SDN课程阅读作业(2)
    2019 SDN上机第4次作业
    第05组 团队Git现场编程实战
    第05组 团队项目-需求分析报告
    团队项目-选题报告
    第07组 Alpha事后诸葛亮
  • 原文地址:https://www.cnblogs.com/gossip/p/3831770.html
Copyright © 2011-2022 走看看