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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM