简体   繁体   English

如何投放DbSet <Team> 到DbSet <EntityBase>

[英]How to cast DbSet<Team> to DbSet<EntityBase>

I have a RepositoryBase class that does some basic operations on Entities which are derived from EntityBase. 我有一个RepositoryBase类,该类对从EntityBase派生的实体进行一些基本操作。

public class RepositoryBase
    {
        private ApplicationDbContext _context;
        public DbSet<EntityBase> DbSet;

        public RepositoryBase(ApplicationDbContext context )
        {
            _context = context;

        }

        public List<EntityBase> List(int entityStatus)
        {
            return DbSet.Where(a => a.EntityStatusId == entityStatus).OrderBy(a => a.Name).ToList();
        }


        public EntityBase GetTeamDetails(int id)
        {
            return this.DbSet.FirstOrDefault(a => a.Id == id);

        }

        internal List<EntityBase> ListActive()
        {
            return DbSet.Where(a => a.EntityStatusId == (int)EntityStatus.Active).ToList();
        }

        internal List<EntityBase> ListTrashed()
        {
            return DbSet.Where(a => a.EntityStatusId == (int)EntityStatus.Trashed).ToList();
        }

        public void SaveProject(EntityBase item)
        {
            DbSet.Attach(item);
            _context.Entry(item).State = EntityState.Modified;
            _context.SaveChanges();

        }

        public List<EntityBase> ListArchived()
        {
            return DbSet.Where(a => a.EntityStatusId == (int)EntityStatus.Archived).ToList();
        }

        internal List<EntityBase> Search(string searchText)
        {
            return DbSet.Where(a => a.Name.Contains(searchText)).ToList();
        }

        public EntityBase New(EntityBase item)
        {
            DbSet.Add(item);
            _context.SaveChanges();
            return item;
        }
    }

The issue I am having is passing the DbSet to the base class. 我遇到的问题是将DbSet传递给基类。 This is what I want to do: 这就是我想做的:

public TeamRepository(ApplicationDbContext context) : base(context)
{
            this.DbSet = (DbSet<EntityBase>)context.Teams;
}

However, this does not compile. 但是,这不会编译。 It says it cannot cast from DbSet to DbSet even through Team is derived from EntityBase. 它说即使从Team派生自EntityBase,也无法将其从DbSet强制转换为DbSet。

How do I make this cast? 如何制作此演员表?

You can't do it like that. 你不能那样做。 What you can do is: 您可以做的是:

this.DbSet = context.Set<EntityBase>();

you can make it generic this way: 您可以通过以下方式使其通用:

public class BaseRepository<T> : IBaseRepository<T> where T : EntityBase
{
    public BaseRepository(CustomDBContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context", "The given parameter cannot be null.");
        }
        this.Context = context;
    }

    protected ApplicationDbContext Context
    {
        get;
        private set;
    }

    public T Get(int id)
    {
        return Context.Set<T>().Find(id);
    }


   .....

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM