EF 7 Migrations with multiple DBC texts

I have a problem with forests and are migrating from a class library with multiple DBContexts. I found a command line argument that looks like this for migration:

dnx ef migration add -c Contexts.IndustryContext initial 

But this does not even work out using the command line parser. I want all my DBContexts and databases to be from the main MVC 6 web project and in their own DLLs. Is it possible? What command line mask is required?

0
entity-framework-core
source share
3 answers

I was looking for the 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 DbContext . 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: this version is the latest at the time of publication and is likely to change in the future.

Then I created a new console application (.NET Core) called Migrations.Console and added it to my solution. You will need to reference all of your DbContext class DbContext in this project.

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

In the Migrations.Console application, I created a DbContextFactory class for every Db context that 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 suit your project.

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

Now to create the transfer, now use the following command:

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

Note. Change the ApplicationDbContext to match the name of the context you are working with. In addition, if you called the console project with a different name, you will need to change the path and name.

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

+4
source share

The new 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 
+1
source share

I have not tried to put the data layer in a separate project yet, but I have several DbContexts in one web API project. He must also work with individual projects.

Using the latest syntax (v1.0.0), you create your migrations as follows:

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

Where <context> is the fully qualified class name of your DbContext - for example, MyProject.Data.MyDbContext

I migrated my migrations for different contexts to different directories, but I don’t think what you need. Just make sure they have different migration-name values.

+1
source share

All Articles