Mik*_*ins 6 entity-framework-core asp.net-core entity-framework-migrations
我有一个多项目解决方案,我无法从主项目成功添加数据库迁移University.Application
。数据库上下文在程序集中定义University.Infrastructure
。
当尝试dotnet ef migrations add Initial
在 University.Application 项目目录中时,我收到以下错误:
Your target project 'University.Application' doesn't match your migrations assembly 'University.Infrastructure'. 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("University.Application")). 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 的主项目中定义了迁移程序集:
services.AddEntityFrameworkSqlServer()
.AddDbContext<UniversityContext>(options =>
{
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
...
Run Code Online (Sandbox Code Playgroud)
在我的 University.Infrastruct 项目中,我定义了一个 UniversityDbContextDesignFactory (我使用两个不同的连接字符串进行运行时和设计):
public class UniversityContextDesignFactory : IDesignTimeDbContextFactory<UniversityContext>
{
public UniversityContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<UniversityContext>()
.UseSqlServer("Server=localhost,1433;Database=UniversityDb;User Id=sa;Password=Pass@word;");
return new UniversityContext(optionsBuilder.Options, new NoMediator());
}
...
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?我尝试按照官方文档进行操作,但没有成功。任何帮助将非常感激。
我有同样的问题,但解决方法却截然不同。我的配置服务与你的不同:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDbContext>(option =>
{
option.UseSqlServer(Configuration.GetConnectionString("ConnectionStringName")); //I set this on appsettings.json
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的 appsettings.json 以防万一您需要它,这只是一个示例:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"Message": "Hello World from AppSetting.JSON",
"ConnectionStrings": {
"ConnectionStringName": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DBNameHere;Integrated Security=True;"
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我正在解决方案目录中执行下面的代码,该目录显然不包含 EF Core 文件,并且不在需要填充迁移文件的目录 ( Data Access Layer folder
) 上。您的情况可能有所不同,但请务必更改文件所在的文件夹DbContext
。
因此,我在命令提示符下将目录更改为我的Data Access Layer
文件夹,然后就是我执行下面的代码的时间,因为我的启动项目位于Core.Web
文件夹上,这也是包含我的Startup.cs
文件的文件夹。( -s means startup
)
dotnet ef migrations add initialcreate -s ..\Core.Web\Core.Web.csproj
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9918 次 |
最近记录: |