Reputation: 2324
I have a multi project solution, where i cannot successfully add a Db migration from the main project University.Application
. The Db context is defined in the assembly University.Infrastructure
.
When attempting dotnet ef migrations add Initial
in the University.Application project dir, i receive the following error:
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.
I have, in the main project in the Startup.cs defined the migrations assembly:
services.AddEntityFrameworkSqlServer()
.AddDbContext<UniversityContext>(options =>
{
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
...
And in my University.Infrastructure project i have defined a UniversityDbContextDesignFactory (I'm using two different connection strings for runtime and design):
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());
}
...
What am i missing here? I tried following the official documentation without luck. Any help would be much appreciated.
Upvotes: 6
Views: 10715
Reputation: 9171
I have the same issue but the fix is quite different. My ConfigureServices is different from yours:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDbContext>(option =>
{
option.UseSqlServer(Configuration.GetConnectionString("ConnectionStringName")); //I set this on appsettings.json
});
}
This is my appsettings.json just in case you need it, this is just an example:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"Message": "Hello World from AppSetting.JSON",
"ConnectionStrings": {
"ConnectionStringName": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DBNameHere;Integrated Security=True;"
}
}
The problem is that I am executing the code below on my solution directory which obviously doesn't contains EF Core files and not on the directory that needs to populate the Migration files (Data Access Layer folder
). Your case might be different but be sure to change your folder where your DbContext
file resides.
So I changed my directory to my Data Access Layer
folder on the command prompt, then that's the time I execute the code below since my startup project is on Core.Web
folder and this is also the folder that contains my Startup.cs
file. (-s means startup
)
dotnet ef migrations add initialcreate -s ..\Core.Web\Core.Web.csproj
Upvotes: 5