ASP.NET vNext – EntityFramework 7.0 Database First (RC1)
This is the updated version using RC1 for https://jefferytay.wordpress.com/2015/11/04/asp-net-vnext-entityframework-7-0-database-first/
Changes from beta8 are colored in red
EF 7.0 supports both code first aka POCO as well as database first. However you do need a bit of tweaking in order to get EF to generate the classes for you. Take note that edmx files are no longer used for EF 7.0.
Step by step guide
- Starting with a blank project, add the following dependencies
Which should make your dependencies look like this
- Visual Studio 2015 should then run nuget in the background to obtain the new libraries. Once that is done, your dependencies list in VS should show the RC1 libraries
You will also need to add the following item into your list of commands
Which will then make your commands list look like
- Now you need to run a command prompt and browse to the project directory, and then run the following command, replacing the brackets with actual data
dnx ef dbcontext scaffold –c [DBContextName] –o [Output folder] [SQLConnection string] EntityFramework.SqlServer
dnx ef dbcontext scaffold -c TestDbContext -o Models "Data Source=.; Initial Catalog=Test;User ID=myusername;Password=P@ssw0rd" EntityFramework.MicrosoftSqlServer
Common errors that you will typically encounter when you see “One or more errors occurred.”:
Take note that the stack trace is VERY long, so you will need to scroll right to the front to see the error message
System.AggregateException: One or more errors occurred. —>Microsoft.Data.Entity.Design.OperationException: Unable to find design-time provider assembly. Please install the EntityFramework.SqlServer.Design NuGet package and ensure that the package is referenced by the project. —> System.IO.FileNotFoundException: Could not load file or assembly ‘EntityFramework.SqlServer.Design’ or one of its dependencies. The system cannot find the file specified.
-> this means project.json does not have the EntityFramework.SqlServer.Design, save the project file and do a dnu restore to ensure that all the packages are restored
System.AggregateException: One or more errors occurred. —>System.Data.SqlClient.SqlException: Login failed for user ‘myusername’.
-> obvious, means you entered the wrong password or are connecting to the wrong database server