执行Add-Migration Initial报错
执行Add-Migration Initial的时候,报错如下:
Your target project 'XX.XXX.Web' doesn't match your migrations assembly 'XX.XXX.DataBase'. 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("XX.XXX.Web")). 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.
解决办法:
在Startup.cs文件里面
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //第一种方法 //services.AddDbContext<TPLMSDbContext>(Options => Options.UseSqlServer(Configuration.GetConnectionString("TPLMSDbContext"))); //第二种方法 services.AddDbContext<TPLMSDbContext>(options=>options.UseSqlServer("server=.\\sqlexpress;database=XXX;uid=sa;pwd=*****", b => b.MigrationsAssembly("XX.XXX.Web"))); services.AddMvc(); }
使用第二种方法
然后在执行:Add-Migration Initial