简体   繁体   English

第二次迁移时实体框架核心编译器错误

[英]Entity Framework Core compiler errors on second migration

I have a DBContext (inheriting from IdentityDbContext) holding the ASP.NET Identity tables and some custom tables. 我有一个DBContext(继承自IdentityDbContext),持有ASP.NET标识表和一些自定义表。

I created a migration for it which worked fine. 我为它创建了一个很好的迁移。

Now I added some DBsets to the DbContext and called 现在我将一些DBset添加到DbContext并调用

dotnet ef migrations add SecondMigration ...

This results in 2 compiler errors after the migration was created. 创建迁移后,这会导致2个编译器错误。

Duplicate 'DbContext' attribute

and

Type 'IdDbContext' already defines a member called 'BuildModel' with the same paramter types.

I don't get it why this is because I did the exact same thing in a different project (for a different dbcontext not inheriting from identitydbcontext) two days ago and it worked without problems. 我不明白为什么这是因为我在两天前在另一个项目(对于不继承自identitydbcontext的不同dbcontext)中做了完全相同的事情并且它没有问题。

This thing is driving me already a bit crazy right now ... 这件事让我现在已经有点疯狂......

Any ideas what could be the cause that the second time I get the compiler errors? 任何想法可能是第二次我得到编译器错误的原因?

And finally I figured out the reason. 最后我弄明白了原因。 Unfortunatly not the source of the problem but at least a fix that works manually. 不幸的是,不是问题的根源,但至少是一个手动工作的修复。

The add migrations command creates a namespace for each migrations. add migrations命令为每次迁移创建一个名称空间。

So I have ...Migrations.FirstMigration and ...Migrations.SecondMigration 所以我有...... Migrations.FirstMigration和...... Migrations.SecondMigration

During the creation of the second migration it creates the files 在创建第二次迁移期间,它会创建文件

  • Timestamp_SecondMigration.cs Timestamp_SecondMigration.cs
  • Timestamp_SecondMigration.Designer.cs Timestamp_SecondMigration.Designer.cs
  • IdDbContextModelSnapshot.cs IdDbContextModelSnapshot.cs

The first two are created in the namespace SecondMigration but for some reason it creates the third file in the namespace FirstMigration. 前两个是在名称空间SecondMigration中创建的,但由于某种原因,它在名称空间FirstMigration中创建了第三个文件。 That causes the problem. 这导致了问题。

Just change the namespace from FirstMigration to SecondMigration and the compiler errors are gone... 只需将命名空间从FirstMigration更改为SecondMigration,编译器错误就消失了......

Hopefully this does not have any negative side effects I don't know yet... 希望这没有任何负面的副作用我还不知道......


If you have 1 DbContext with 3 migrations, should there be 1 ModelSnapshot or should there be 3 ModelSnapshots? 如果你有1个带有3次迁移的DbContext,那么应该有1个ModelSnapshot还是应该有3个ModelSnapshots?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM