ApplicationDbContext未实现接口

时间:2018-10-24 15:44:07

标签: c# asp.net-mvc asp.net-identity

我首先使用代码来开发ASP.NET MVC解决方案。 我在ApplicationDbContext中遇到此错误

ApplicationDbContext

ApplicationUser类没有问题

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类,这里是我遇到的问题:

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。 我已经花了几个小时,但仍然无法解决问题。我在哪里弄错了,我该怎么办?

1 个答案:

答案 0 :(得分:3)

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

SaveChanges的定义也不正确,因为它可能会返回int

重构接口以使用IDbSet<>接口代替Users属性,并且让SaveChanges返回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;
}

并相应地更新具体类以匹配界面

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);
    }
}