Target project doesn't match migrations assembly

asp.net-core ef-migrations entity-framework-core

Question

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.

1
1
6/11/2018 8:22:16 PM

Popular Answer

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
0
4/7/2020 10:01:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow