.net - 自创建数据库以来,支持 'ApplicationDbContext' 上下文的模型已更改

标签 .net asp.net-mvc linq entity-framework asp.net-mvc-5

首先,我没有在其他地方看到过这个错误,我猜这不是重复的,所以请先阅读整个情况。

一切都工作正常,然后我尝试更新我的模型类之一(应用程序类,更新现在已注释),我将在下面列出;我遇到了这个丑陋的错误。

<小时/>

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269). at System.Data.Entity.CreateDatabaseIfNotExists1.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf1.b__e() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.Include(String path) at System.Data.Entity.Infrastructure.DbQuery1.Include(String path) at System.Data.Entity.QueryableExtensions.Include[T](IQueryable1 source, String path) at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable1 source, Expression1 path) at Microsoft.AspNet.Identity.EntityFramework.UserStore6.GetUserAggregateAsync(Expression1 filter) at Microsoft.AspNet.Identity.EntityFramework.UserStore6.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager2.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager`2.d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at ControlPanel.Web.Controllers.AccountController.d__2.MoveNext() in d:\Projects\FULL\Control Panel\ControlPanel.Web\Controllers\AccountController.cs:line 56

起初我认为这可能是迁移问题,因此我完全删除了数据库,重新启用了迁移,并添加了 Init 迁移并使用更新了数据库

update-database -force -verbose

一切都很顺利,没有任何提示,但是,每当我尝试登录我的网站时,我都会收到之前的错误。我进行了大约十次迁移,但未能解决问题。

这是我的域类(模型):

public class App
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int AppId { get; set; }
    //[Required]
    public virtual string FacebookId { get; set; }
    //[Required]
    public virtual string Secret { get; set; }      
    public virtual List<User> Users { get; set; }
    public virtual List<Post> Posts { get; set; }      
    //public virtual ApplicationUser Admin { get; set; }
}

public class Post
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int PostId { get; set; }
    public virtual string Content { get; set; }
    public virtual string Link { get; set; }
    public virtual string Image { get; set; }
    public virtual bool IsSpecial { get; set; }
    //[Required]
    public virtual App App { get; set; }
    //[Required]
    public virtual DateTime? PublishDate { get; set; }
}

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int UserId { get; set; }

    [MaxLength(500)]
    public virtual string FacebookId { get; set; }

    [MaxLength(500)]
    public virtual string Token { get; set; }

    //[Required]
    public virtual App App { get; set; }
}

这是我的 IdentityModel:

public class ApplicationUser : IdentityUser
{
    public virtual List<App> Apps { get; set; }
    public bool? IsPremium { get; set; }
    [DataType(DataType.Date)]
    public DateTime? LastPublishDateTime { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("dCon")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("Admins");
        modelBuilder.Entity<ApplicationUser>().ToTable("Admins");
        modelBuilder.Entity<IdentityUserRole>().ToTable("AdminRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("Logins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("Claims");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    }
}

最佳答案

以防万一其他人偶然发现这个问题,就像我一样首先进行数据库实现。

我通过扩展 ApplicationUser 类进行了更改,向 AspNetUsers 表添加了一个新字段,然后在启动时出现此错误。

我能够通过删除在 __MigrationHistory 表中创建的记录(那里只有一条记录)来解决这个问题,我假设 EF 决定我需要使用迁移工具更新我的数据库 - 但是我已经自己手动完成了此操作。

关于.net - 自创建数据库以来,支持 'ApplicationDbContext' 上下文的模型已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22408608/

相关文章:

.net - 是否可以在 VC++/CLI 中重写 IEnumerable?

c# - 我的控制台应用程序中的上下文菜单发生了什么?

.net - 什么可能导致死锁或以其他方式导致此并发测试不一致地失败?

c# - 使用 LINQ 的 SQL 表行总和?

LINQ 分组/子查询来填充层次结构数据结构

asp.net-mvc - 如何添加具有需要由 Autofac 解决的依赖项的自定义 ModelMetadataDetailsProvider?

asp.net - ASP.NET MVC 中的 Post Action 分页问题

c# - 从 ASP.NET MVC 创建打开的 XML Excel 工作表时出错

c# - Linq自连接查询

c# - 关于 Linq to SQL 映射对象设计的建议