我正在做一個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'"
我不t know why I get this error, when I try to execute the command with the
-assembly`參數。
我無法從其他程序集創建初始遷移,我搜索了有關它的信息,但沒有得到任何結果。
有人有類似的問題?
您是否閱讀過文檔? https://docs.efproject.net/zh/latest/miscellaneous/cli/dotnet.html?highlight=workaround#targeting-class-library-projects-is-not-supported – Tseng
是的,但我沒有得到解決錯誤,我嘗試了不同的命令選項--startup-project和--assebly,但我沒有得到任何東西 – kdar
不要使用--assembly。這是一個應該從幫助信息中隱藏的內部實現細節,但是由於https://github.com/aspnet/EntityFramework/issues/5188 – natemcmaster