EF Core 2.0 I am a believer
- Create a new dotnet Core MVC application
- Install the tools for EF Core 2.0
- Scaffold the database
Create a new dotnet core MVC app
Install the tools for EF Core 2.0
Open the app, here using VSCode but can be Visual Studio whatever. Code or Visual Studio should take care of the initial dotnet cor packages necessary for a web project. We will need the following packages
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.SqlServer.Design
- Microsoft.EntityFrameworkCore.Tools.DotNet (dotnet ef commands) or Microsoft.EntityFrameworkCore.Tools (powershell )
DotNet CLI
Install-Package <packageName>
Looking for a correction here not sure how to add the tooling except by editing the csproj file directly it should look as follows:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
```
Finally Scaffolding the database
Scaffold-DbContext -Connection "Server=localhost;Database=AdventureWorks2016CTP3;Integrated Security=true;" -Provider Microsoft.EntityFrameworkCore.SqlServer -Context AdventureWorksCtx -OutputDir .Models -DataAnnotations
Scaffold-DbContext comand we just ran gets all the POCO (Plain Old Code Objects) from the database, as well as the dbcontext and associated metadata about the tables and columns that the system can discern
- Once the command is run you will get a verbose output
- Find the Context we referenced in the Scaffold command, here we called it AdventureWorksCtx
Just past the DbSets for each of the tables you will find two important pieces of information- The Tables that could not be created and why
- Below the table issues is the connection string we should make into either an Azure Secret or at a minimum move to a settings file
References