ApplicationDbContext 不实现接口

ApplicationDbContext Does Not Implement Interface

我正在研究 ASP.NET 首先使用代码的 MVC 解决方案。 我的 ApplicationDbContext

中出现了这个错误

ApplicationUser class 没有问题

public class ApplicationUser : IdentityUser, IDeletableEntity
{
    public bool IsDeleted { get; set; }

    public DateTime? DeletedOn { get; set; }

    public string DeletedBy { get; set; }

    public string ImageUrl { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here
        return userIdentity;
    }
}

下面也是我的IApplicationDbContext

using System.Data.Entity;
using System.Data.Entity.Infrastructure;

using SmartSIMS.Models.Entities.UserManagement.Models;
using SmartSIMS.Models;

public interface IApplicationDbContext
{
    DbSet<ApplicationUser> Users { get; set; }

    void SaveChanges();

    DbSet<TEntity> Set<TEntity>() where TEntity : class;

    DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
}

然后,最后是 ApplicationDbContext class,我遇到的问题是:

namespace SmartSIMS.Data.DBContext.Concrete
{

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            //Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        #region ACADEMIC

        #region Academics
        public DbSet<Assignment> Assignments { get; set; }
        public DbSet<AssignmentSubmission> AssignmentSubmissions { get; set; }

        public new void SaveChanges()
        {
            try
            {
                base.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }

        public new DbSet<TEntity> Set<TEntity>() where TEntity : class
        {
            return base.Set<TEntity>();
        }
    }
}

我在 Visual Studio 2017 年使用 ASP.NET MVC 5。 我已经花了几个小时,但仍然无法解决问题。我哪里做错了,我该怎么办?

您的 IApplicationDbContext 暴露了 Users 属性 的错误类型,它是 IDbSet<> 接口而不是 DbSet<> class。

SaveChanges 也没有正确定义,因为它假设 return 和 int

重构接口以使用 IDbSet<> 接口代替 Users 属性 并且还有 SaveChanges return 和 int

public interface IApplicationDbContext {
    IDbSet<ApplicationUser> Users { get; set; }

    int SaveChanges();

    DbSet<TEntity> Set<TEntity>() where TEntity : class;
    DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
}

并相应地更新具体 class 以匹配接口

public int SaveChanges()
{
    try
    {
        return base.SaveChanges();
    }
    catch (DbEntityValidationException ex)
    {
        // Retrieve the error messages as a list of strings.
        var errorMessages = ex.EntityValidationErrors
                .SelectMany(x => x.ValidationErrors)
                .Select(x => x.ErrorMessage);

        // Join the list to a single string.
        var fullErrorMessage = string.Join("; ", errorMessages);

        // Combine the original exception message with the new one.
        var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

        // Throw a new DbEntityValidationException with the improved exception message.
        throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
    }
}