相关疑难解决方法(0)

使用不同的程序集添加迁移

我正在ASP.NET CORE 1.0.0中做一个项目,我正在使用EntityFrameworkCore.我有单独的程序集,我的项目结构如下所示:

ProjectSolution
   -src
      -1 Domain
         -Project.Data
      -2 Api
         -Project.Api
Run Code Online (Sandbox Code Playgroud)

在我Project.ApiStartup课堂上

public void ConfigureServices(IServiceCollection services)
    {            
        services.AddDbContext<ProjectDbContext>();

        services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<ProjectDbContext>()
                .AddDefaultTokenProviders();
    }
Run Code Online (Sandbox Code Playgroud)

DbContext是我Project.Data项目

public class ProjectDbContext : IdentityDbContext<IdentityUser>
{
    public ProjectDbContext(DbContextOptions<ProjectDbContext> options) : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {

        var builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());
        builder.AddJsonFile("appsettings.json");
        IConfiguration Configuration = builder.Build();

        optionsBuilder.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection"));
        base.OnConfiguring(optionsBuilder);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试进行初始迁移时,出现此错误:

"您的目标项目'Project.Api'与迁移程序集'Project.Data'不匹配.更改目标项目或更改迁移程序集.使用DbContextOptionsBuilder更改迁移程序集.例如options.UseSqlServer(connection,b = > b.MigrationsAssembly("Project.Api")).默认情况下,迁移程序集是包含DbContext的程序集.使用程序包管理器控制台的"默认项目"下拉列表或执行将目标项目更改为迁移项目来自包含迁移项目的目录中的"dotnet ef"."

看到此错误后,我尝试执行以下命令Project.Data:

dotnet ef --startup-project ../Project.Api --assembly"../../1 Data/Project.Data"迁移添加初始 …

c# ef-migrations entity-framework-core asp.net-core

34
推荐指数
9
解决办法
3万
查看次数