Add migration with different assembly

后端 未结 14 1168
感情败类
感情败类 2020-12-02 14:25

I am working on a project with ASP.NET CORE 1.0.0 and I am using EntityFrameworkCore. I have separate assemblies and my project structure looks like this:

Pr         


        
相关标签:
14条回答
  • 2020-12-02 14:45

    I ran on the same problem and found this

    We’re you trying to run your migrations on a class library? So was I. Turns out this isn’t supported yet, so we’ll need to work around it.

    EDIT: I found solution on this git repo

    0 讨论(0)
  • 2020-12-02 14:46

    (ASP.NET Core 2+)

    Had the same issue. Here is what I did:

    1. Reference the project that contains the DbContext (Project.A) from the project that will contain the migrations (Project.B).

    2. Move the existing migrations from Project.A to Project.B (If you don't have migrations - create them first)

    3. Configure the migrations assembly inside Project.A

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

    Assuming your projects reside in the same parent folder:

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

    The migrations now go to Project.B

    Source: Microsoft

    0 讨论(0)
  • 2020-12-02 14:46

    There are multiple projects included in the Solution.

    Solution
    |- MyApp (Startup Proj)
    |- MyApp.Migrations (ClassLibrary)

    Add-Migration NewMigration -Project MyApp.Migrations
    

    Note: MyApp.Migrations also includes the DbContext.

    0 讨论(0)
  • 2020-12-02 14:47

    Add Migration With CLI Command:

    dotnet ef migrations add NewMigration --project YourAssemblyName
    

    Add Migration With PMC Command:

    Add-Migration NewMigration -Project YourAssemblyName
    
    0 讨论(0)
  • 2020-12-02 14:48

    All EF commands have this check:

    if (targetAssembly != migrationsAssembly) 
           throw MigrationsAssemblyMismatchError;
    

    targetAssembly = the target project you are operating on. On the command line, it is the project in the current working directory. In Package Manager Console, it is whatever project is selected in the drop down box on the top right of that window pane.

    migrationsAssembly = assembly containing code for migrations. This is configurable. By default, this will be the assembly containing the DbContext, in your case, Project.Data.dll. As the error message suggests, you have have a two options to resolve this

    1 - Change target assembly.

    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 - Change the migrations assembly.

    cd Project.Api/
    dotnet ef migrations add Initial
    
    // change the default migrations assembly
    options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api"))
    
    0 讨论(0)
  • 2020-12-02 14:49

    Using EF Core 2, you can easily separate your Web project from your Data (DbContext) project. In fact, you just need to implement the IDesignTimeDbContextFactory interface. According to Microsoft docs, IDesignTimeDbContextFactory is:

    A factory for creating derived DbContext instances. Implement this interface to enable design-time services for context types that do not have a public default constructor. At design-time, derived DbContext instances can be created in order to enable specific design-time experiences such as Migrations. Design-time services will automatically discover implementations of this interface that are in the startup assembly or the same assembly as the derived context.

    In the bottom code snippet you can see my implementation of DbContextFactory which is defined inside my Data project:

    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);
        }
    }
    

    Now, I can initialize EF migration by setting my Web project as the StartUp project and selecting my Data project inside the Package Manager Console.

    Add-Migration initial
    

    You can find more details here. However, this blog post uses an obsoleted class instead of IDesignTimeDbContextFactory.

    0 讨论(0)
提交回复
热议问题