EF 7 Migrations with multiple DBContexts

后端 未结 3 1518
夕颜
夕颜 2021-01-06 14:15

I have a problem scaffolding and doing migrations from a class library with multiple DBContexts. I found a command line argument that looks like this for migrations:

相关标签:
3条回答
  • 2021-01-06 14:54

    I haven't tried putting the data-layer in a separate project yet, but I do have multiple DbContexts in a single Web API project. It should work with separate projects too.

    Using the latest syntax (v1.0.0), you create your migrations like this:

    dotnet ef migrations add <migration-name> -o <output-directory> -c <context>
    

    Where <context> is the full class-name of your DbContext -- like MyProject.Data.MyDbContext

    I put my migrations for different contexts into different directories, but I don't think you have to. Just make sure they have different migration-name values.

    0 讨论(0)
  • 2021-01-06 15:00

    I was looking for an answer to this question and wanted to provide my solution for ef core 2.0.

    Microsoft.EntityFrameworkCore.Tools.DotNet needs to be added to each of your class libraries that have a DbContext in them. Right click the project and select Edit *.csproj. Then, add the following:

      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
      </ItemGroup>
    

    Note: the version is the latest at the time of this post and will likely change in the future.

    Next, I created a new Console App (.NET Core) called Migrations.Console and added it to my Solution. You will have to reference all of your DbContext class libraries in this project.

    I installed Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design Nuget packages.

    In the Migrations.Console app, I created a DbContextFactory class for each Db context I have.

    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Design;
    
    public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
            builder.UseSqlServer("Server=(local);Database=DATABASENAME;Trusted_Connection=True;MultipleActiveResultSets=true");
            return new ApplicationDbContext(builder.Options);
        }
    }
    

    Note: Make sure you update the context and connection string to match your project.

    Now that each DbContextFactory is created, you can start to create the migrations. Go to the folder for your class library. The easiest way it to right click the project and Open Folder in File Explorer. Then, type cmd in the address bar of the File Explorer to open a command prompt in that folder.

    Now use the following command to create the migration:

    dotnet ef migrations add InitialCreate -c ApplicationDbContext --startup-project ../Migrations.Console/Migrations.Console.csproj

    Note: Change ApplicationDbContext to match the name of the context you are working with. Also, if you called the console project by another name, you will need to change the path and name.

    You should now see a Migrations folder in your class library.

    0 讨论(0)
  • 2021-01-06 15:12

    The newest version (RC1 update 1) of the command line tools supports the following syntax:

    Usage: dnx ef dbcontext scaffold [arguments] [options]
    
    Arguments:
      [connection]  The connection string of the database
      [provider]    The provider to use. For example, EntityFramework.MicrosoftSqlServer
    
    Options:
      -a|--data-annotations                 Use DataAnnotation attributes to configure the model where possible. If omitted, the output code will use only the fluent API.
      -c|--context <name>                   Name of the generated DbContext class.
      -o|--output-dir <path>                Directory of the project where the classes should be output. If omitted, the top-level project directory is used.
      -s|--schema <schema_name.table_name>  Selects a schema for which to generate classes.
      -t|--table <schema_name.table_name>   Selects a table for which to generate classes.
      -p|--target-project <project>         The project to scaffold the model into. If omitted, the current project is used.
      -e|--environment <environment>        The environment to use. If omitted, "Development" is used.
      -v|--verbose                          Show verbose output
      -?|-h|--help                          Show help information
    
    0 讨论(0)
提交回复
热议问题