【问题标题】:Can Set () method in the Repository having reference to my DBcontext可以参考我的 DBcontext 在存储库中设置()方法
【发布时间】:2016-12-25 21:42:58
【问题描述】:

我有一个简单的通用存储库。

在这个存储库中,我有一个通用 DBContext,这意味着它不特定于我的应用程序。

通用 DbContext 仍然能够访问我的 DBcontexts 实体,例如

Context.Set<TEntity>().Add(entity)

这里的方法 Set() 是否引用了我的 webapplikationens DBContext,或者发生了什么?

完整代码:

    public interface IRepository<TEntity> where TEntity : class
    {
        TEntity Get(int id);
        void Add(TEntity entity);
    }

...

    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly DbContext Context;

        public Repository(DbContext context)
        {
            Context = context;
        }

        public TEntity Get(int id)
        {
            // Here we are working with a DbContext, not PlutoContext. So we don't have DbSets 
            // such as Courses or Authors, and we need to use the generic Set() method to access them.
            //Returns a non-generic DbSet instance for access to entities of the given type in the context and the underlying store .
            return Context.Set<TEntity>().Find(id);
        }

        public void Add(TEntity entity)
        {
            Context.Set<TEntity>().Add(entity);
        }
    }
.................................

    public interface IAuthorRepository : IRepository<Author>
    {
        Author GetAuthorWithCourses(int id);
    }
..

    public class AuthorRepository : Repository<Author>, IAuthorRepository
    {
        public AuthorRepository(PlutoContext context) : base(context)
        {
        }

        public Author GetAuthorWithCourses(int id)
        {
            return PlutoContext.Authors.Include(a => a.Courses).SingleOrDefault(a => a.Id == id);
        }

        public PlutoContext PlutoContext
        {
            get { return Context as PlutoContext; }
        }
    }
..


    public interface IUnitOfWork : IDisposable
    {
        IAuthorRepository Authors { get; }
        int Complete();
    }
...

  public class UnitOfWork : IUnitOfWork
    {
        private readonly PlutoContext _context;

        public UnitOfWork(PlutoContext context)
        {
            _context = context;
           Authors = new AuthorRepository(_context);
        }

        public IAuthorRepository Authors { get; private set; }

        public int Complete()
        {
            return _context.SaveChanges();
        }

        public void Dispose()
        {
            _context.Dispose();
        }
    }


...

        public class Repository<Entity> : IRepository<Entity> where Entity : class
        {
        protected readonly DbContext Context;

        public Repository(DbContext context)
        {
            Context = context;
        }


        public void Add(Entity entity)
        {
            Context.Set<TEntity>().Add(entity);
        }
    }

【问题讨论】:

  • Set() 将返回在您注入存储库的上下文子类型中注册的任何实体。
  • 如果我们想象我用这个实体“House”注入我的通用存储库。这一行,Context.Set()Find(id);将在我的Repository 中蜜蜂翻译成:Context.House()。查找(id);但是她的上下文仍然是通用的,我不应该将其转换为我的应用程序 DBContext 吗?
  • 参数DbContext context只是一个compile-time类型,参数的actual类型没有改变。但是你为什么不试试呢?通常看到它,有助于理解它。
  • 我试过了,效果很好。我只是不明白它为什么起作用。

标签: c# asp.net-mvc entity-framework generics model-view-controller


【解决方案1】:

参数 DbContext context 只是一个编译时类型,参数的实际类型没有改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2015-02-24
    • 2019-08-17
    • 1970-01-01
    • 2023-03-25
    • 2012-02-23
    • 2019-09-16
    相关资源
    最近更新 更多