Entity Framework 7 Database First Configuration (MVC 6)

After a long constant struggle, it finally became clear how to use the first approach to the EF7 database using MVC 6. Here's how to do it:

App.Impl -> project.json:

"frameworks": { "net451": { }, "dnx451": { } }, "dependencies": { "EntityFramework.Commands": "7.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final" }, 

Then run the command from cmd:

 dnx ef dbcontext scaffold "Server=ip,port;Database=db;User Id=user;Password=pass;" EntityFramework.MicrosoftSqlServer 

3 problems that I encountered. In my project Impl.

  • I have a folder dedicated to Data. The My Data folder should contain all things related to the database, such as DbContext, DbClasses. However, Scaffold creates these files at the root. How can I specify the folder in which I want these files to be created?

  • In my workplace, I could have a database with 50 tables. However, if I only need access to 2? I do not want to add all 50 tables for my project. How can I specify which tables I want?

  • In my database, I could have a table called "Users" because it is a table containing users. However, the scaffold must create the class as a "User", since it is the only user until it becomes a List. How can I specify the name of the table to be created?

Thank you all for your help.

+3
source share
1 answer

Try using

 dnx ef dbcontext scaffold "Server=Server\InstanceName;Database=db;Trusted_Connection=True;" EntityFramework.MicrosoftSqlServer --dataAnnotations --outputDir Data --verbose --table dbo.Users 

All the above parameters should be on one line, but I wrapped a long line to read it more easily. You can look at the source code to see which options support the scaffold command in RC1.

Be careful with copying and pasting ConnectionString from appsettings.json , because you may have Server=Server\\InstanceName; in ConnectionString , but dnx ef dbcontext scaffold now only accepts Server=Server\InstanceName; and you will get a System.InvalidOperationException error when using Server=Server\\InstanceName; copied directly from ConnectionString from appsettings.json .

An additional important parameter is -p | --targetProject -p | --targetProject , which is important if you use the repository in the class library. In case you define the ef command in the main project and run dnx ef dbcontext scaffold in the main project directory, but use -p to refer to the class library project.

Last remark. Sometimes you need to tint a subset of tables from a database. This is not completely clear from the command line help, but you can specify the -t ( -table ) -table several times . See the note on the wiki EF7. Thus, if you want to import only two tables, dbo.Users and dbo.Posts (regardless of whether Posts foreign key for Users ), you can use the following syntax

 dnx ef dbcontext scaffold "Server=Server\InstanceName;Database=db;Trusted_Connection=True;" EntityFramework.MicrosoftSqlServer -a -o Models -v -t dbo.Users -t dbo.Posts 

UPDATED: In ASP.NET Core RC2 and later, you should use dotnet ef dbcontext scaffold instead of dnx ef dbcontext scaffold .

+6
source

All Articles