我正在使用 ASP.NET CORE 1.0.0 开发一个项目,并且我正在使用 EntityFrameworkCore。我有单独的程序集,我的项目结构如下所示:
ProjectSolution
-src
-1 Domain
-Project.Data
-2 Api
-Project.Api
在我的 Project.Api
中是 Startup
类
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ProjectDbContext>();
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ProjectDbContext>()
.AddDefaultTokenProviders();
}
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);
}
}
当我尝试进行初始迁移时,出现此错误:
"Your target project 'Project.Api' doesn't match your migrations assembly 'Project.Data'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."
看到这个错误后,我尝试执行位于 Project.Api
中的命令:
dotnet ef --startup-project ../Project.Api --assembly "../../1 Data/Project.Data" migrations add Initial
我得到了这个错误:
"Unexpected value '../../1 Domain/Project.Data' for option 'assembly'"
当我尝试使用“-assembly”参数执行命令时,我不知道为什么会出现此错误。
我无法从其他程序集创建初始迁移,我搜索了有关它的信息但没有得到任何结果。
有人遇到过类似的问题吗?
最佳答案
所有 EF 命令都有 this check :
if (targetAssembly != migrationsAssembly)
throw MigrationsAssemblyMismatchError;
targetAssembly = 您正在操作的目标项目。在命令行中,它是当前工作目录中的项目。在包管理器控制台中,它是在该窗口 Pane 右上角的下拉框中选择的任何项目。
migrationsAssembly = 包含迁移代码的程序集。这是可配置的。默认情况下,这将是包含 DbContext 的程序集,在您的例子中是 Project.Data.dll。 如错误消息所示,您有两种选择来解决此问题
1 - 更改目标程序集。
cd Project.Data/
dotnet ef --startup-project ../Project.Api/ migrations add Initial
// code doesn't use .MigrationsAssembly...just rely on the default
options.UseSqlServer(connection)
2 - 更改迁移程序集。
cd Project.Api/
dotnet ef migrations add Initial
// change the default migrations assembly
options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api"))
关于c# - 添加具有不同程序集的迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38705694/