How can I generate the Context database by command?

0

I'm doing a simple application in .net core and entity framework core, I want to separate the project by layers to work more organized, my questions are the following

1) To the solution do I have to add a .net core class library? or a standard .net class library?

2) How can I generate the dbcontext file using scaffold by commands in my new layer?

    
asked by Maria Perez 26.04.2018 в 21:22
source

1 answer

0

Greetings, I recommend that you use .NET Standard since it will generate a library compatible with the majority of .NET implementations (.NET Framework, dotnet core and xamarin).

Here they explain more details: link

Regarding the generation of your dbcontext using scaffold

If you use Visual Studio you can follow these instructions to do so with NuGet Package Manager Console

1.Install-Package Microsoft.EntityFrameworkCore.SqlServer

2.Install-Package Microsoft.EntityFrameworkCore.Tools

Then you use this command to generate your database

Scaffold-DbContext "Server=Sqlsever;Database=dbname;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Microsoft Documentation here

If you use Visual Studio Code it would be using dotnet CLI

Your project should look like this

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>
</Project>

To install these packages you must use dotnet add package.

After having the project in this way without any error you use this command:

dotnet ef dbcontext scaffold "Server=Server;Database=Dbname;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer  --startup-project ..\ProyectoNETCore  -o Models

Remember that it is important to specify the project to start , which must be a .net core project from another you should add the .net core framework to the .netstandard project, because in order for this to work it must if there is a project using .net core somewhere

Official documentation here

Related link

    
answered by 29.04.2018 / 06:15
source