用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

EF 三层架构 简单示例

2016-09-21 作者: 疯信子举报

[c#]代码库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{

    public partial class Test2Entities : System.Data.Entity.DbContext
    {
        public Test2Entities()
            : base("name=Test2Entities")
        {
            Database.SetInitializer<Test2Entities>(new System.Data.Entity.CreateDatabaseIfNotExists<Test2Entities>());
        }

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            //modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        }

        public virtual DbSet<ModelL.RoleSet> RoleSet { get; set; }
        public virtual DbSet<ModelL.UserSet> UserSet { get; set; }
    }

    public static class GetDbContext
    {
        static Test2Entities _Test2DbContext = new Test2Entities();
        public static Test2Entities Test2DbContext
        {
            get { return _Test2DbContext; }
        }
    }

    public class RepositoryBase<T> : ModelL.IRepository<T> where T : class, new()
    {
        Test2Entities _Test2DbContext = GetDbContext.Test2DbContext;
        public T Create()
        {
            return _Test2DbContext.Set<T>().Create();
        }

        public T Update(T entity)
        {
            return entity;
        }

        public T Insert(T entity)
        {
            return _Test2DbContext.Set<T>().Add(entity);
        }

        public void Delete(T entity)
        {
            _Test2DbContext.Set<T>().Remove(entity);
        }

        public ModelL.ReturnMsg SaveChange()
        {
            ModelL.ReturnMsg rm = new ModelL.ReturnMsg();
            try
            {
                int i = _Test2DbContext.SaveChanges();
                rm.ErrCode = 0;
                rm.Successful = true;
                rm.ErrMsg = "";

            }
            catch (Exception exc)
            {
                rm.ErrCode = exc.HResult;
                rm.Successful = false;
                rm.ErrMsg = exc.Message;
            }
            return rm;
        }

        public T Find(params object[] keyValues)
        {
            return _Test2DbContext.Set<T>().Find(keyValues);
        }

        public T Find(Func<T, bool> predict)
        {
            return _Test2DbContext.Set<T>().FirstOrDefault(predict);
        }

        public List<T> FindAll(Func<T, bool> predict)
        {
            return _Test2DbContext.Set<T>().Where(predict).ToList();
        }

        public List<T> FindAll()
        {
            return _Test2DbContext.Set<T>().ToList();
        }
    }

    public class RoleRepository : DAL.RepositoryBase<ModelL.RoleSet>
    {

    }

    public class UserRepository : DAL.RepositoryBase<ModelL.UserSet>
    {

    }

}

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...