Entity Framework 7 Database First configuration (MVC 6)

前端 未结 2 945
情话喂你
情话喂你 2020-12-11 09:28

After a long constant struggle, finally figured out how to use EF7 Database first approach using MVC 6. This is how it goes:

App.Impl -> project.json:



        
相关标签:
2条回答
  • 2020-12-11 09:45

    Try to use

    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 in the same line, but I wrapped the long line to read it more easy. You can look at the source code to see which options supports scaffold command in RC1.

    Be carefully in copy and paste the ConnectionString from appsettings.json because you could have Server=Server\\InstanceName; in ConnectionString, but dnx ef dbcontext scaffold accept currently only Server=Server\InstanceName; and you will get System.InvalidOperationException error on the usage of Server=Server\\InstanceName; directly copied from the ConnectionString of appsettings.json.

    Additional important parameter is -p | --targetProject, which is important if you use repository in the class library. In the case you defines ef command in the main project, and you start dnx ef dbcontext scaffold in the directory of the main project, but you use -p to reference the class library project.

    The last remark. Sometimes one need to scaffold subset of tables from the database. It's not full clear from the command line help, but one can specify -t (-table) parameter multiple times. See the note in the EF7 wiki. Thus if you want to import only two tables dbo.Users and dbo.Posts (whether Posts have foreign key to Users) then 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: One should use dotnet ef dbcontext scaffold instead of dnx ef dbcontext scaffold in ASP.NET Core RC2 and later.

    0 讨论(0)
  • 2020-12-11 09:51

    I also wanted to specify a handful of tables. I followed Oleg's explanation for showing how to import multiple tables in one command. But .Net Core 3.1, in the Visual Studio Package Manager Console, that way gives the error - Cannot bind parameter because parameter 'Tables' is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3".

    So Oleg's syntax would have to be changed to - t dbo.Users,dbo.Products,dbo.Orders

    My full command was from the Package Manager Console is -

    Scaffold-DbContext "Data Source=servername;Initial Catalog=dbname;User ID=usn;PWD=mypw;Connect Timeout=100" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -t dbo.Users,dbo.Products,dbo.Orders

    0 讨论(0)
提交回复
热议问题