C# 实体框架核心添加迁移抛出;已添加具有相同密钥的项。关键字:MyProject.Model.MyContext
我的项目中有一个带有EF核心的SQL Server数据库,我想在另一个表中添加一个表和一列。但是当我在PM控制台中运行C# 实体框架核心添加迁移抛出;已添加具有相同密钥的项。关键字:MyProject.Model.MyContext,c#,entity-framework,entity-framework-core,entity-framework-migrations,C#,Entity Framework,Entity Framework Core,Entity Framework Migrations,我的项目中有一个带有EF核心的SQL Server数据库,我想在另一个表中添加一个表和一列。但是当我在PM控制台中运行addmigration时,我得到了这个错误 已添加具有相同密钥的项。关键字:MyProject.Model.MyContext 其中,MyProject是我的asp.net核心项目,MyContext是我的DbContext类 我的DbContext类看起来像 namespace MyProject.Model { public class MyContext : Db
addmigration
时,我得到了这个错误
已添加具有相同密钥的项。关键字:MyProject.Model.MyContext
其中,MyProject
是我的asp.net核心项目,MyContext
是我的DbContext类
我的DbContext类看起来像
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();
}
}
}
你能分享实体的结构吗?通常在EF迁移错误中有关于特定问题的更多信息;你能看一下并贴出来吗?您还可以向命令提供一个-Verbose标志。使用输出编辑问题看起来像是找到了实际上下文
MyContext
两次;也许您从重构中知道在哪里可以找到它。还可以尝试显式清除解决方案的所有obj和bin文件夹,然后重新生成。它通过扫描“启动”程序集查找实现IDesignTimeDbContextFactory
的任何内容来查找工厂。尝试将其添加到您的MyProject
,尽可能靠近启动,设置调试器断点,运行(应用程序)并检查结果以查看是否得到一个或两个值:IEnumerable contextFactories=Assembly.getExecutionGassembly().DefinedTypes.Where(t=>!t.Isastract&!t.IsGenericTypeDefinition)。其中(t=>typeof(IDesignTimeDbContextFactory).GetTypeInfo().IsAssignableFrom(t))
您可以共享实体的结构吗?通常在EF迁移错误中有关于特定问题的更多信息;您可以查看并发布它吗?您还可以向命令提供一个-Verbose标志。使用outputIt编辑问题看起来像是找到了实际上下文MyContext
两次;可能从重构过程中,您知道应该在哪里查找。还可以尝试显式清理解决方案的所有obj和bin文件夹,然后重新构建。它通过扫描“启动”来查找工厂任何实现IDesignTimeDbContextFactory
的程序集。请尝试将其添加到MyProject
,尽可能靠近启动,设置调试器断点,运行(应用程序)并检查结果以查看是否得到一个或两个值:IEnumerable ContextFactorys=assembly.GetExecutiveGassembly().DefinedTypes.Where(t=>!t.isasStract&&!t.IsGenericTypeDefinition).Where(t=>typeof(IDesignTimeDbContextFactory).GetTypeInfo().IsAssignableFrom(t));
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