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;
            }
        }
    }
  • 相关阅读:
    基于 Laravel 完整开源项目大全
    微信分享 php jssdk
    点点客投票post抓包
    php一些 get就能提交的漏洞
    彩票网的数据接口 分析
    PHP一些实际常用的代码
    ThinkCMF常用代码
    php解压zip 待优化
    Python调用pywin32模块 发送QQ消息,打印窗口标题,切换窗口等句柄
    浅谈数列分块问题
  • 原文地址:https://www.cnblogs.com/gossip/p/3831770.html
Copyright © 2011-2022 走看看