ASP.NET 引入存储库模式导致“IdentityUserLogin”没有键定义错误

问题描述

我开始在我的 ASP.NET 应用程序中实现存储库模式,但是在尝试在我已经实现模式的视图上运行操作时,我得到 'EntityType 'IdentityUserLogin' has no key defined。定义此 EntityType 的键。'​​。当我没有实现存储库时,应用程序上不会发生这种情况。

MeetRepository.cs

using MVCWebAssignment1.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MVCWebAssignment1.DAL
{
    public class MeetRepository : IMeetRepository
    {
        private readonly MeetContext _context;
        
        public MeetRepository(MeetContext meetContext)
        {
            _context = meetContext;
        }
        public IList<Meet> GetMeets()
        {
            return _context.Meets.ToList();
        }

        public Meet GetMeetById(int id)
        {
            return _context.Meets.Find(id);
        }

        public void InsertMeet(Meet meet)
        {
            _context.Meets.Add(meet);
        }

        public void DeleteMeet(int id)
        {
            Meet meet = _context.Meets.Find(id);
            _context.Meets.Remove(meet);
        }

        public void UpdateMeet(Meet meet)
        {
            _context.Entry(meet).State = EntityState.Modified;
        }
        
        public void Save()
        {
            _context.SaveChanges();
        }
        private bool disposed = false;
        protected virtual void dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                {
                    _context.dispose();
                }
            }
        }

        public void dispose()
        {
            dispose(true);
            GC.SuppressFinalize(this);
        }
    }

IMeetRepository,cs

using MVCWebAssignment1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCWebAssignment1.DAL
{
    public interface IMeetRepository : Idisposable
    {
        IList<Meet> GetMeets();
        Meet GetMeetById(int id);
        void InsertMeet(Meet meet);
        void DeleteMeet(int id);
        void UpdateMeet(Meet meet);
        void Save();
    }
}

MeetContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MVCWebAssignment1.Models;

namespace MVCWebAssignment1.DAL
{
    public class MeetContext : DbContext
    {
        public MeetContext(): base("name=DefaultConnection"){}
        public DbSet<Meet> Meets { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

MeetController.cs(标记问题的方法

    public class MeetController : Controller
    {
        //private ApplicationDbContext db = new ApplicationDbContext();

        private IMeetRepository _meetRepository;

        public MeetController()
        {
            _meetRepository = new MeetRepository(new MeetContext());
        }

        // GET: Meet
        public ActionResult Index()
        {
            return View(_meetRepository.GetMeets());
        }
    }

总而言之,当我引入的类不使用身份组件时,我无法确定为什么使用存储库模式会开始创建身份文件的问题。

非常感谢指导!

解决方法

经过进一步的试验,我已经解决了这个问题。我通过在我的存储库上下文中继承 IdentityDbContect 解决了这个问题。

MeetContext.cs

 public class MeetContext : IdentityDbContext<ApplicationUser>
    {
        public MeetContext(): base("DefaultConnection"){}
        public DbSet<Meet> Meets { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}