In previous to 7 versions of the Entity Framework all Entity Framework related commands could be executed within Visual Studio. Now you have to use the new ASP .NET 5 dnx command instead.
DbContext Model Migration from Entity Framework 6
In order to migrate my model from Entity Framework 6 to Entity Framework 7, I did not want to manually adjust my old DbContext for Entity Framework 7. Instead I used scaffolding from my existing database to create a completely new model. This minimizes the risk of introducing errors in the new model. After I generated the model, I deleted the previous Migration History table in the database. This might not be suitable for everyone, but I wanted to make a clean cut after transitioning to EF 7.
Scaffold DbContext
In order to scaffold a DBContext from an existing database, you first have to set up dnx ef. If you have not done so already, please read the 2 previous blog entries about ASP .NET 5.
- https://www.illucit.com/blog/2016/02/asp-net-5-toolchain-setup/
- https://www.illucit.com/blog/2016/02/asp-net-5-with-entity-framework-7/
To be more specific, the examples in this article were done using Entity Framework 7 RC1. If you execute dnx ef in your ASP .NET project folder, you will get the following output.
dnx ef output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
_/\__ ---==/ \\ ___ ___ |. \|\ | __|| __| | ) \\\ | _| | _| \_/ | //|\\ |___||_| / \\\/\\ Entity Framework Commands 7.0.0-rc1-16348 Usage: dnx ef [options] [command] Options: --version Show version information -?|-h|--help Show help information Commands: database Commands to manage your database dbcontext Commands to manage your DbContext types migrations Commands to manage your migrations Use "dnx ef [command] --help" for more information about a command. |
dnx ef dbcontext output:
1 2 3 4 5 6 7 8 9 10 |
Usage: dnx ef dbcontext [options] [command] Options: -?|-h|--help Show help information Commands: list List your DbContext types scaffold Scaffolds a DbContext and entity type classes for a specified database Use "dbcontext [command] --help" for more information about a command. |
dnx ef dbcontext scaffold output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Missing required argument '[connection]' 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|--dataAnnotations 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|--outputDir <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|--targetProject <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 |
So in order to scaffold a DbContext from an existing database you simply have to execute the following command.
1 |
dnx ef dbcontext scaffold -o outputDir "Data Source=Your ConnectionString" EntityFramework.MicrosoftSqlServer |
In my example I am using a Microsoft SQL Server and outputDir as the directory for storing the generated DbContext. In the outputDir you will find one generated class for each table in your database and a newly generated DbContext. If you have additional tables in the database, that you actually do not want to use in your DbContext e.g. if you are using external tools that use the same database, then simply remove the corresponding files and references from DbContext. If you are scaffolding a DbContext from an existing Entity Framework 6 database, you should remove the class generated for the previous Migration History table __MigrationHistory. By the way, the new table for the Migration History is called slightly different i.e. __EFMigrationsHistory.
Summary
This articles demonstrated how to migrate a DbContext from Entity Framework 6 to 7 or how to generate a DbContext from a completely new database. In the next article we will show how to do Code first migrations with Entity Framework 7.
Hi,
How can I scaffold existing views in the database ? For the moment, only the tables are scaffolded …