自创建数据库以来,支持“ApplicationDbContext"上下文的模型已更改 [英] The model backing the 'ApplicationDbContext' context has changed since the database was created
问题描述
一切正常,然后我尝试更新我的一个模型类(应用类,更新现在留下评论),我将在下面列出;和繁荣我有这个丑陋的错误.
Everything was working just fine then I tried to update one of my model classes ( the App class and the update is now left commented ) which I will be listing below; and boom I had this ugly error.
自创建数据库以来,支持ApplicationDbContext"上下文的模型已更改.考虑使用 Code First 迁移来更新数据库 (http://go.microsoft.com/fwlink/?LinkId=238269).在 System.Data.Entity.CreateDatabaseIfNotExists1.InitializeDatabase(TContext context) 在 System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf
1.b__e() 在 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(Action
1 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.InternalSet
1.Include(字符串路径) 在 System.Data.Entity.Infrastructure.DbQuery1.Include(String path) 在 System.Data.Entity.QueryableExtensions.Include[T](IQueryable
1 source, String path) at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable1 source, Expression
1 path)) 在 Microsoft.AspNet.Identity.EntityFramework.UserStore6.GetUserAggregateAsync(Expression
1 filter) 在 Microsoft.AspNet.Identity.EntityFramework.UserStore6.FindByNameAsync(String userName) 在 Microsoft.AspNet.Identity.UserManager
2.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager`2.d__12.MoveNext() --- 从上一个抛出异常的位置结束堆栈跟踪---在系统.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at ControlPanel.Web.Controllers.AccountController.d__2.MoveNext() in d:ProjectsFULLControl PanelControlPanel.WebControllersAccountController.cs:line 56
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.CreateDatabaseIfNotExists
1.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf
1.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(Action
1 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.InternalSet
1.Include(String path) at System.Data.Entity.Infrastructure.DbQuery1.Include(String path) at System.Data.Entity.QueryableExtensions.Include[T](IQueryable
1 source, String path) at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable1 source, Expression
1 path) at Microsoft.AspNet.Identity.EntityFramework.UserStore6.GetUserAggregateAsync(Expression
1 filter) at Microsoft.AspNet.Identity.EntityFramework.UserStore6.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager
2.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:ProjectsFULLControl PanelControlPanel.WebControllersAccountController.cs:line 56
起初我认为这可能是一个迁移问题,所以我完全删除了数据库,重新启用了迁移,并添加了一个初始化迁移并使用
At first I thought it might be a migrations problem, so I dropped the database entirely, re-enabled the migrations, and added an Init migration and updated the database using
update-database -force -verbose
一切顺利,没有任何抱怨,但是,每当我尝试登录我的网站时,我都会收到上一个错误.我做了十次左右迁移的事情都没有解决问题.
Everything goes well with no complaints, however, whenever I try to log in to my site I get the previous error. I did the migration thing about ten times without being able to solve the problem.
这是我的领域类(模型):
Here are my domain classes ( models ):
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:
Here are my IdentityModels:
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");
}
}
推荐答案
这是一个很奇怪的错误,最后不是我的错误,是微软的,我安装了实体框架预发布"版本,它是导致此错误的原因,当我升级到稳定版本时它消失了,谢谢大家相信我,当我问这个问题时,我搜索了一周左右的解决方案,所以我很确定问题不在其他任何地方:如果有帮助,导致问题的实体 framework.dll 版本是 6.0.2.
It was such a strange error,, It wasn't my error at the end, it was Microsoft's,, I installed the Entity framework the " pre-release" version and it was responsible for this error,, when i upgraded to the stable release it disapperared,, thank you everyone believe me when i asked this question i searched like for a week or so for its solution so i am pretty sure that this problem isn't anywhere else : the version of entity framework.dll that caused the problem was 6.0.2 if it helps.
这篇关于自创建数据库以来,支持“ApplicationDbContext"上下文的模型已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!