Add migration with another assembly

I am doing a project in ASP.NET CORE 1.0.0 and I am using EntityFrameworkCore. I have separate assemblies, and my project structure is as follows:

ProjectSolution
   -src
      -1 Domain
         -Project.Data
      -2 Api
         -Project.Api

I Project.Apihave a class in myStartup

public void ConfigureServices(IServiceCollection services)
    {            
        services.AddDbContext<ProjectDbContext>();

        services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<ProjectDbContext>()
                .AddDefaultTokenProviders();
    }

DbContext is in my project 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);
    }
}

When I try to perform the initial migration, I get this error:

" " Project.Api " " Project.Data ". , . DbContextOptionsBuilder. . options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")). , DbContext. "dotnet ef" , . "

, , , 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'"

, , -assembly.

, , .

- ?

+30
10

EF :

if (targetAssembly != migrationsAssembly) 
       throw MigrationsAssemblyMismatchError;

targetAssembly= , . . , .

migrationsAssembly= , . . , DbContext, , Project.Data.dll. ,

1 - .

cd Project.Data/
dotnet ef --startup-project ../Project.Api/ migrations add Initial

// code doesn't use .MigrationsAssembly...just rely on the default
options.UseSqlServer(connection)

2 - .

cd Project.Api/
dotnet ef migrations add Initial

// change the default migrations assembly
options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api"))
+46

, , => " " "Project.Data", "Project.API".

"Project.Data" , .

default project selection

+21

EF Core 2, - Data (DbContext). , IDesignTimeDbContextFactory. Microsoft, IDesignTimeDbContextFactory :

DbContext. , , . DbContext , , . , , .

DbContextFactory, :

public class DbContextFactory : IDesignTimeDbContextFactory<KuchidDbContext>
{
    public KuchidDbContext CreateDbContext(string[] args)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

        var dbContextBuilder = new DbContextOptionsBuilder<KuchidDbContext>();

        var connectionString = configuration.GetConnectionString("Kuchid");

        dbContextBuilder.UseSqlServer(connectionString);

        return new KuchidDbContext(dbContextBuilder.Options);
    }
}

EF, - .

Add-Migration initial

. IDesignTimeDbContextFactory.

+10

this

? . , , .

EDIT: git repo

+5

, EF , .

- , :

EF CLI . --data-dir, .

, , :

  • use -OutputDir command parameter,.e.g., Add-Migration InitConfigurationStore -OutputDir PersistedStores/ConfigurationStore mgiration PersistedStores/ConfigurationStore .

10/12/2017

public void ConfigureServices(IServiceCollection services)
{
    ...

    string dbConnectionString = services.GetConnectionString("YOUR_PROJECT_CONNECTION");
    string assemblyName = typeof(ProjectDbContext).Namespace;

    services.AddDbContext<ProjectDbContext>(options =>
        options.UseSqlServer(dbConnectionString,
            optionsBuilder =>
                optionsBuilder.MigrationsAssembly(assemblyName)
        )
   );

   ...
}
+5

(ASP.NET Core 2+)

. :

  1. , DbContext (Project.A), , (Project.B).

  2. Project.A Project.B ( - )

  3. Project.A

options.UseSqlServer( ConnectionString, x => x.MigrationsAssembly("Project.B"));

, :

  1. dotnet ef migrations add Init --project Project.B -c DbContext

Project.B

: Microsoft

+3
Add-Migration NewMigration -Project MyApp.Migrations
+1

, , . @Ehsan Mirsaeedi, DbContextFactory. Startup API, DbContextFactory, Data ( ).

public class DbContextFactory : IDesignTimeDbContextFactory<KuchidDbContext>
{
   public KuchidDbContext CreateDbContext(string[] args)
   {
       var configuration = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json")
          .Build();

       var dbContextBuilder = new DbContextOptionsBuilder<KuchidDbContext>();

       var connectionString = configuration.GetConnectionString("connectionString");

       var migrationAssemblyName= configuration.GetConnectionString("migrationAssemblyName");

       dbContextBuilder.UseSqlServer(connectionString, o => o.MigrationAssembly(migrationAssemblyName));

       return new KuchidDbContext(dbContextBuilder.Options);
   }
}

SetBasePath AddJsonFile "Microsoft.Extensions.Configuration" "Microsoft.Extensions.Configuration.Json".

: , . DbContextOptions , . , .

0

@Kaloyan Drenski . Asp.net Core 2.2

0

, .

, - Project.Api ( ) .

, - :)

-1

Source: https://habr.com/ru/post/1649819/


All Articles