简体   繁体   English

Asp.net核心通用存储库模式,继承不起作用

[英]Asp.net core generic repository pattern with inheritance not working

I have created a generic repository class and interface which will be subclassed for the concrete repositories. 我创建了一个通用的存储库类和接口,它将被子类化为具体的存储库。

Here is some of my code: 这是我的一些代码:

The generic IRepository interface and Repository class: 通用的IRepository接口和Repository类:

public interface IRepository<T> where T : BaseEntity
{
    IEnumerable<T> GetAll();
    T Get(long id);
    void Insert(T entity);
    void Update(T entity);
    void Delete(T entity);
}

public class Repostitory<T> : IRepository<T> where T : BaseEntity
    {
        protected readonly ApplicationDbContext _context;
        private DbSet<T> _entities;
        private string errorMessage = string.Empty;

        public Repostitory(ApplicationDbContext context)
        {
            this._context = context;
            _entities = context.Set<T>();
        }

        public IEnumerable<T> GetAll()
        {
            return _entities.AsEnumerable();
        }

        public T Get(long id)
        {
            return _entities.SingleOrDefault(s => s.Id == id);
        }

        public void Insert(T entity)
        {
            CheckEntityNotNull(entity);
            _entities.Add(entity);
            _context.SaveChanges();
        }

        public void Update(T entity)
        {
            CheckEntityNotNull(entity);
            _context.SaveChanges();
        }

        public void Delete(T entity)
        {
            CheckEntityNotNull(entity);
            _entities.Remove(entity);
            _context.SaveChanges();
        }

        private void CheckEntityNotNull(T entity)
        {
            if(entity == null)
            {
                throw new ArgumentNullException("Entity");
            }
        }
    }

Inherited Interface and class: 继承的接口和类:

public interface IEventsRepository : IRepository<Event>
{

}

public class EventsRepository : Repostitory<Event>
{
    public EventsRepository(ApplicationDbContext context) : base(context)
    {

    }
}

At this moment there is no implementation yet in IEventsRepository and EventsRepository , but there will be once I got this to work. 正在这时,有又是在没有实现IEventsRepositoryEventsRepository ,但会有一次我得到了这个工作。

In Startup.cs I have this in the ConfigureServices method Startup.cs我在ConfigureServices方法中有这个

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddScoped(typeof(IRepository<>), typeof(Repostitory<>));
    services.AddScoped(typeof(IEventsRepository), typeof(EventsRepository));
}

And in the controller: 在控制器中:

public class AdminController : Controller
{
    private readonly IEventsRepository _eventsRepository;

    public AdminController(IEventsRepository eventsRepository)
    {
        _eventsRepository = eventsRepository;
    }

    [HttpGet]
    public IActionResult EventsIndex()
    {
        var model = _eventsRepository.GetAll();
        return View(model);
    }
}

When building the application I do not get any errors, or even when I open the homepage. 在构建应用程序时,我没有收到任何错误,甚至在我打开主页时也是如此。

When I open the EventsIndex page an internal server error is raised: 当我打开EventsIndex页面时,会引发内部服务器错误:

InvalidCastException: Unable to cast object of type 'WebsiteProject.Repositories.EventsRepository`1[WebsiteProject.Models.Event]' to type 'WebsiteProject.Repositories.IEventsRepository`1[WebsiteProject.Models.Event]'.

What am I missing here? 我在这里错过了什么?

I was able to fix it. 我能够解决它。 It was quite simple actually. 实际上很简单。

All I had to do is make EventsRepository also implement IEventsRepository . 所有我必须做的是使EventsRepository也实现了IEventsRepository

So instead of 而不是

public class EventsRepository : Repostitory<Event>
{
    public EventsRepository(ApplicationDbContext context) : base(context)
    {

    }
}

I changed it to: 我改成了:

public class EventsRepository : Repostitory<Event>, IEventsRepository
{
    public EventsRepository(ApplicationDbContext context) : base(context)
    {

    }
}

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

相关问题 Asp.Net Core 通用存储库模式软删除 - Asp.Net Core Generic Repository Pattern Soft Delete ASP.NET 5 MVC 6通用存储库模式 - ASP.NET 5 MVC 6 Generic Repository Pattern 在 ASP.NET 核心中使用 Inheritance 与使用通用存储库的依赖注入 - Using Inheritance vs Dependency injection with generic Repository in ASP.NET Core 如何在我的 ASP.NET 核心 Web API 项目中使用工作单元模式和通用存储库模式? - How can I use unit of work pattern with generic repository pattern in my ASP.NET Core Web API project? ASP.Net 核心:通用 Controller &amp; Model Inheritance - ASP.Net Core : Generic Controller & Model Inheritance asp.net-ef-通用服务/存储库模式 - asp.net - ef - generic service/repository pattern 使用 Autofac ASP.Net Core 5 注册动态通用存储库 - Registering dynamic generic repository with Autofac ASP.Net Core 5 ASP.NET 核心 - 可能 Null 通用存储库中的参考返回 - ASP.NET Core - Possible Null Reference Return in Generic Repository 在ASP.NET Core中使用存储库模式中的接口和抽象类 - Using interface and abstract class in repository pattern in ASP.NET Core 用于发布实体的 Asp.Net 核心存储库和工作单元模式 - Asp.Net core Repository and Unit Of Work Pattern for Posting an entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM