c# - Entity Framework Core Add-Migration throws "An item with the same key has already been added. Key: MyProject.Model.MyContext
问题描述
I have a SQL Server Database with EF Core in my project and I want to add a table and a column in another table. But when I run Add-Migration
in the PM Console I get this Error
An item with the same key has already been added. Key: MyProject.Model.MyContext
where MyProject
is my asp.net core project and MyContext
is my DbContext class.
My DbContext class looks like
namespace MyProject.Model
{
public class MyContext : DbContext
{
public MyWorker Worker { get; private set; }
/// <summary>
/// initialize the worker instance to get the crud methods of the database
/// </summary>
public MyContext()
{
Worker = new MyWorker(this);
}
public static string GetConnectionString()
{
return Startup.ConnectionString;
}
/// <summary>
/// Our own context must override the OnConfiguring Method from Ef Core to set the right connection
/// Connectionstring is set in appsettings.json
/// </summary>
/// <param name="_builder"></param>
protected override void OnConfiguring(DbContextOptionsBuilder _builder)
{
_builder.UseSqlServer(GetConnectionString());
}
protected override void OnModelCreating(ModelBuilder _modelBuilder) {
_modelBuilder.Entity<User>().HasIndex(u => u.MailAdress).IsUnique();
}
public DbSet<Project> Projects { get; set; }
public DbSet<Document> Documents { get; set; }
public DbSet<DocData> DocsData { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UCLSystem> Systems { get; set; }
}
public class DesignTimeContextFactory : IDesignTimeDbContextFactory<MyContext>
{
public MyContext CreateDbContext(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<MyContext>();
var connectionString = configuration["Connectionstrings:MyConnection"];
return new MyContext();
}
}
}
The table to add is the Systems
Table and the two columns to add are in the documents
table.
With the -verbose
flag I get the following output
PM> Add-Migration Update -verbose
Using project 'MyProject'.
Using startup project 'MyProject'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile D:\Development\Project\MyProject\MyProject\bin\Debug\netcoreapp2.2\MyProject.deps.json --additionalprobingpath C:\Users\chris\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig D:\Development\Project\MyProject\MyProject\bin\Debug\netcoreapp2.2\MyProject.runtimeconfig.json C:\Users\chris\.nuget\packages\microsoft.entityframeworkcore.tools\2.2.4\tools\netcoreapp2.0\any\ef.dll migrations add Update --json --verbose --no-color --prefix-output --assembly D:\Development\Project\MyProject\MyProject\bin\Debug\netcoreapp2.2\MyProject.dll --startup-assembly D:\Development\Project\MyProject\MyProject\bin\Debug\netcoreapp2.2\MyProject.dll --project-dir D:\Development\Project\MyProject\MyProject\ --language C# --working-dir D:\Development\Project\MyProject --root-namespace MyProject
Using assembly 'MyProject'.
Using startup assembly 'MyProject'.
Using application base 'D:\Development\Project\MyProject\MyProject\bin\Debug\netcoreapp2.2'.
Using working directory 'D:\Development\Project\MyProject\MyProject'.
Using root namespace 'MyProject'.
Using project directory 'D:\Development\Project\MyProject\MyProject\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'DesignTimeContextFactory'.
Found DbContext 'MyContext'.
Found IDesignTimeDbContextFactory implementation 'DesignTimeContextFactory'.
Found DbContext 'MyContext'.
System.ArgumentException: An item with the same key has already been added. Key: MyProject.Model.MyContext
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String name)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An item with the same key has already been added. Key: MyProject.Model.MyContext
解决方案
就我而言,我需要删除生成的快照 CS 文件并重新生成迁移。
推荐阅读
- scikit-learn - 在 Scikit Learn 中运行 SelectKBest 后,“numpy.ndarray”对象没有属性“get_support”错误消息
- escaping - Power Query:从内部有很多引号的长字符串中提取包含引号的子字符串
- java - 我该怎么做这个文件路径?
- python - 如何将一些新变量添加到张量流中加载的检查点?
- asp.net-core - 如何从中间件获取 ASPNETCORE OData 请求的路由数据?
- c - 修复与通用指针相关的警告
- angular - Angular5如何使用Http Observable
- bash - Linux shell:如何删除字符串中特定字符之前/之后的前导/尾随字符?
- javafx - 为什么 JavaFX PrintJob 无法正确打印节点的文本?
- azure - 'Microsoft.Compute' Azure 资源提供者永远不会完成'注册':