[C#] EF 三层架构 简单示例 →→→→→进入此内容的聊天室

来自 , 2019-03-13, 写在 C#, 查看 148 次.
URL http://www.code666.cn/view/9813b270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DAL
  8. {
  9.  
  10.     public partial class Test2Entities : System.Data.Entity.DbContext
  11.     {
  12.         public Test2Entities()
  13.             : base("name=Test2Entities")
  14.         {
  15.             Database.SetInitializer<Test2Entities>(new System.Data.Entity.CreateDatabaseIfNotExists<Test2Entities>());
  16.         }
  17.  
  18.         protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
  19.         {
  20.             //modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
  21.         }
  22.  
  23.         public virtual DbSet<ModelL.RoleSet> RoleSet { get; set; }
  24.         public virtual DbSet<ModelL.UserSet> UserSet { get; set; }
  25.     }
  26.  
  27.     public static class GetDbContext
  28.     {
  29.         static Test2Entities _Test2DbContext = new Test2Entities();
  30.         public static Test2Entities Test2DbContext
  31.         {
  32.             get { return _Test2DbContext; }
  33.         }
  34.     }
  35.  
  36.     public class RepositoryBase<T> : ModelL.IRepository<T> where T : class, new()
  37.     {
  38.         Test2Entities _Test2DbContext = GetDbContext.Test2DbContext;
  39.         public T Create()
  40.         {
  41.             return _Test2DbContext.Set<T>().Create();
  42.         }
  43.  
  44.         public T Update(T entity)
  45.         {
  46.             return entity;
  47.         }
  48.  
  49.         public T Insert(T entity)
  50.         {
  51.             return _Test2DbContext.Set<T>().Add(entity);
  52.         }
  53.  
  54.         public void Delete(T entity)
  55.         {
  56.             _Test2DbContext.Set<T>().Remove(entity);
  57.         }
  58.  
  59.         public ModelL.ReturnMsg SaveChange()
  60.         {
  61.             ModelL.ReturnMsg rm = new ModelL.ReturnMsg();
  62.             try
  63.             {
  64.                 int i = _Test2DbContext.SaveChanges();
  65.                 rm.ErrCode = 0;
  66.                 rm.Successful = true;
  67.                 rm.ErrMsg = "";
  68.  
  69.             }
  70.             catch (Exception exc)
  71.             {
  72.                 rm.ErrCode = exc.HResult;
  73.                 rm.Successful = false;
  74.                 rm.ErrMsg = exc.Message;
  75.             }
  76.             return rm;
  77.         }
  78.  
  79.         public T Find(params object[] keyValues)
  80.         {
  81.             return _Test2DbContext.Set<T>().Find(keyValues);
  82.         }
  83.  
  84.         public T Find(Func<T, bool> predict)
  85.         {
  86.             return _Test2DbContext.Set<T>().FirstOrDefault(predict);
  87.         }
  88.  
  89.         public List<T> FindAll(Func<T, bool> predict)
  90.         {
  91.             return _Test2DbContext.Set<T>().Where(predict).ToList();
  92.         }
  93.  
  94.         public List<T> FindAll()
  95.         {
  96.             return _Test2DbContext.Set<T>().ToList();
  97.         }
  98.     }
  99.  
  100.     public class RoleRepository : DAL.RepositoryBase<ModelL.RoleSet>
  101.     {
  102.  
  103.     }
  104.  
  105.     public class UserRepository : DAL.RepositoryBase<ModelL.UserSet>
  106.     {
  107.  
  108.     }
  109.  
  110. }

回复 "EF 三层架构 简单示例"

这儿你可以回复上面这条便签

captcha