zoukankan      html  css  js  c++  java
  • EF 5.0 帮助类 增删改查

    原文地址:http://www.cnblogs.com/luomingui/p/3362813.html

    EF 5.0 帮助类

    加入命名空间:

    using System;
    using System.Data;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Linq;

    接口:

    复制代码
    public interface IEFRepository<TEntity> where TEntity : class
        {
            bool AddEntity(TEntity entity);
            bool UpdateEntity(TEntity entity);
            bool UpdateEntity(IEnumerable<TEntity> entities);
            bool DeleteEntity(int ID);
            bool DeleteEntity(TEntity entity);
            bool DeleteEntity(Expression<Func<TEntity, bool>> predicate);
            bool DeleteEntity(IEnumerable<TEntity> entities);
            IList<TEntity> LoadEntities(Func<TEntity, bool> whereLambda);
            IList<TEntity> LoadEntities(int pageIndex = 1, int pageSize = 30, Func<TEntity, bool> whereLambda = null);
            TEntity FindByID(int ID);
        }
    复制代码

    具体类:

    复制代码
    //EF5.0的写法 
        public class EFRepository<TEntity> : IEFRepository<TEntity> where TEntity : class
        {
            #region 单利模式
            public static EFRepository<TEntity> Instance = new EFRepository<TEntity>();
            public EFRepository( )
            {
                Create();
            }
            #endregion
    
            /// <summary>
            /// 获取 当前使用的数据访问上下文对象
            /// </summary>
            public DbContext Context
            {
                get
                {
                    return EFDbContextHelper.Context;
                }
            }
            public bool AddEntity(TEntity entity)
            {
                EntityState state = Context.Entry(entity).State;
                if (state == EntityState.Detached)
                {
                    Context.Entry(entity).State = EntityState.Added;
                }
                Context.SaveChanges();
                return true;
            }
            public bool UpdateEntity(TEntity entity)
            {
                Context.Set<TEntity>().Attach(entity);
                Context.Entry<TEntity>(entity).State = EntityState.Modified;
                return Context.SaveChanges() > 0;
            }
            public bool UpdateEntity(IEnumerable<TEntity> entities)
            {
                try
                {
                    Context.Configuration.AutoDetectChangesEnabled = false;
                    foreach (TEntity entity in entities)
                    {
                        UpdateEntity(entity);
                    }
                    return true;
                }
                finally
                {
                    Context.Configuration.AutoDetectChangesEnabled = true;
                }
            }
            public bool DeleteEntity(int ID)
            {
                TEntity entity = FindByID(ID);
                return DeleteEntity(entity);
            }
            public bool DeleteEntity(TEntity entity)
            {
                Context.Set<TEntity>().Attach(entity);
                Context.Entry<TEntity>(entity).State = EntityState.Deleted;
                return Context.SaveChanges() > 0;
            }
            public bool DeleteEntity(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
            {
                List<TEntity> entities = Set().Where(predicate).ToList();
                return Context.SaveChanges() > 0;
            }
            public bool DeleteEntity(IEnumerable<TEntity> entities)
            {
                try
                {
                    Context.Configuration.AutoDetectChangesEnabled = false;
                    foreach (TEntity entity in entities)
                    {
                        DeleteEntity(entity);
                    }
                    return true;
                }
                finally
                {
                    Context.Configuration.AutoDetectChangesEnabled = true;
                }
            }
            public IList<TEntity> LoadEntities(Func<TEntity, bool> whereLambda)
            {
                if (whereLambda != null)
                    return Context.Set<TEntity>().Where<TEntity>(whereLambda).AsQueryable().ToList();
                else
                    return Context.Set<TEntity>().AsQueryable().ToList();
            }
            public IList<TEntity> LoadEntities(int pageIndex = 1, int pageSize = 30, Func<TEntity, bool> whereLambda = null)
            {
                int skinCount = (pageIndex - 1) * pageSize;
                if (whereLambda != null)
                    return Set()
                        .Where<TEntity>(whereLambda)
                        .Skip(skinCount)
                        .Take(pageSize)
                        .ToList();
                else
                    return Set()
                    .Skip(skinCount)
                    .Take(pageSize)
                    .ToList();
            }
            public DbSet<TEntity> Set( )
            {
                return Context.Set<TEntity>();
            }
            public TEntity FindByID(int ID)
            {
                return Set().Find(ID);
            }
            private TEntity Create( )
            {
                return Context.Set<TEntity>().Create();
            }
        }
    复制代码

    使用:

    准备实体类

    复制代码
    /// <summary> 
        /// 实体类楼层管理 。(属性说明自动提取数据库字段的描述信息) 
        /// </summary> 
        [Serializable]
        public class Floor 
        { 
            public int ID { get; set; } 
            [Category("楼层名称")]
            public string f_Name { get; set; } 
            [Category("备注")]
            public string f_Remark { get; set; } 
        }
    复制代码

    使用EF帮助类调用

    复制代码
    /// <summary> 
        /// 数据上下文 Db3983Context 
        /// </summary> 
        public class Db3983Context : EFDbContext 
        { 
            /// <summary> 
            /// 构造函数 
            /// </summary> 
            public Db3983Context() 
                : base("3983") 
            { 
            } 
            /// <summary> 
            /// 楼层管理 
            /// </summary> 
            public DbSet<Floor> Floor { get; set; } 
    }
    复制代码
    复制代码
    /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main( )
            {
                EFDbContextHelper.Context = new Db3983Context();
    
                Floor floor = new Floor();
                floor.f_Name = "罗敏贵";
                floor.f_Remark = "我这个人看上去很靠谱,长得也很高有一米八五,也很帅气,千万不要迷恋哥,哥只是传说。";
    
                EFRepository<Floor>.Instance.AddEntity(floor);
            }
    复制代码

    扩展:

    其他ORM只要现实上面的接口就可以了,然后在配置文件制定使用哪个ORM就可以做到扩展了。

  • 相关阅读:
    wamp+phpzendstudio配置xdebug57%解决办法
    【linux学习问题解决】使用aptget安装软件出现unable to locate package的解决办法
    【linux LAMP平台安装】写在前面(一)
    【linux学习问题解决】更改字符界面大小(转)
    [phpcms二次开发]phpcms生成栏目出错,转到模版页面
    [phpcms二次开发]给url规则添加可用更多自定义可用变量
    [phpcms二次开发]实现获取路径linux与windows路径兼容
    控件注册 利用资源文件将dll、ocx打包进exe文件(转)
    C#串口通信:MSComm控件使用详解
    改善C#程序的建议1:非用ICloneable不可的理由
  • 原文地址:https://www.cnblogs.com/niaowo/p/3673988.html
Copyright © 2011-2022 走看看