Skip to content

Commit 07281a7

Browse files
author
Anthony Sneed
authored
Merge pull request #86 from TrackableEntities/ef-core-3-preview8
Update to EF Core 3.0.0
2 parents ce698b0 + 38e643c commit 07281a7

File tree

40 files changed

+858
-761
lines changed

40 files changed

+858
-761
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Scaffold EF Core models using Handlebars templates.
66

77
## Prerequisites
88

9-
- [Visual Studio 2017](https://www.visualstudio.com/downloads/) 15.9 or greater.
10-
- The .[NET Core 2.2 SDK](https://www.microsoft.com/net/download/core) (version 2.2.100 or greater).
9+
- [Visual Studio 2019](https://www.visualstudio.com/downloads/) 16.3 or greater.
10+
- The .[NET Core 3.0 SDK](https://www.microsoft.com/net/download/core).
1111

1212
## Database Setup
1313

@@ -21,17 +21,16 @@ Scaffold EF Core models using Handlebars templates.
2121
## Usage
2222

2323
1. Create a new **.NET Core** class library.
24-
- If necessary, edit the csproj file to update the **TargetFramework** to 2.2.
24+
- If necessary, edit the csproj file to update the **TargetFramework** to 3.0.
2525

2626
> **Note**: Using the EF Core toolchain with a _.NET Standard_ class library is currently not supported. Instead, you can add a .NET Standard class library to the same solution as the .NET Core library, then add existing items and select **Add As Link** to include entity classes.
2727
28-
2. Add EF Core SQL Server and Tools NuGet packages.
29-
- Open the Package Manager Console, select the default project and enter:
30-
+ `Install-Package Microsoft.EntityFrameworkCore.SqlServer`
31-
+ `Install-Package Microsoft.EntityFrameworkCore.Design`
28+
2. Add EF Core SQL Server and Tools NuGet packages.
29+
- `Microsoft.EntityFrameworkCore.SqlServer`
30+
- `Microsoft.EntityFrameworkCore.Design`
3231

3332
3. Add the **EntityFrameworkCore.Scaffolding.Handlebars** NuGet package:
34-
- `Install-Package EntityFrameworkCore.Scaffolding.Handlebars`
33+
- `EntityFrameworkCore.Scaffolding.Handlebars`
3534

3635
4. Remove Class1.cs and add a **ScaffoldingDesignTimeServices** class.
3736
- Implement `IDesignTimeServices` by adding a `ConfigureDesignTimeServices` method
@@ -122,14 +121,14 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
122121
## Extending the OnModelCreating Method
123122

124123
There are times when you might like to modify generated code, for example, by adding a `HasConversion` method to an entity property in the `OnModelCreating` method of the generated class that extends `DbContext`. However, doing so may prove futile because added code would be overwritten the next time you run the `dotnet ef dbcontext scaffold` command.
125-
- Rather than modifying generated code, a better idea would be to extend it by using _partial classes and methods_. To enable this scenario, the generated `DbContext` class is already defined using the `partial` keyword, and it contains a partial `OnModelCreatingExt` method that is invoked at the end of the `OnModelCreating` method.
126-
- To implement the partial method, simply add a new class to your project with the same name as the generated `DbContext` class, and define it as `partial`. Then add a `OnModelCreatingExt` method with the same signature as the partial method defined in the generated `DbContext` class.
124+
- Rather than modifying generated code, a better idea would be to extend it by using _partial classes and methods_. To enable this scenario, the generated `DbContext` class is already defined using the `partial` keyword, and it contains a partial `OnModelCreatingPartial` method that is invoked at the end of the `OnModelCreating` method.
125+
- To implement the partial method, simply add a new class to your project with the same name as the generated `DbContext` class, and define it as `partial`. Then add a `OnModelCreatingPartial` method with the same signature as the partial method defined in the generated `DbContext` class.
127126

128127
```csharp
129-
// Place in separate class file (NorthwindSlimContextExt.cs)
128+
// Place in separate class file (NorthwindSlimContextPartial.cs)
130129
public partial class NorthwindSlimContext
131130
{
132-
partial void OnModelCreatingExt(ModelBuilder modelBuilder)
131+
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
133132
{
134133
modelBuilder.Entity<Employee>()
135134
.Property(e => e.Country)
@@ -169,8 +168,13 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
169168

170169
- You can also omit `c` and `--context-dir` arguments from the EF Core scaffolding command.
171170

171+
- Install the global `dotnet ef` tool.
172172
```
173-
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -f
173+
dotnet tool install --global dotnet-ef --version 3.0.0-*
174+
```
175+
- Open a command prompt at the project root and execute:
176+
```
177+
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -c NorthwindSlimContext -f --context-dir Contexts
174178
```
175179

176180

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "2.2.401"
3+
"version": "3.0.100"
44
}
55
}
Binary file not shown.
Binary file not shown.
81.5 KB
Binary file not shown.
Binary file not shown.

sample/ScaffoldingSample.Api/Controllers/EmployeeController.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Linq;
42
using System.Threading.Tasks;
53
using Microsoft.AspNetCore.Mvc;
64
using Microsoft.EntityFrameworkCore;
7-
using ScaffoldingSample.Models;
5+
using ScaffoldingSample.Contexts;
86

97
namespace ScaffoldingSample.Api.Controllers
108
{
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
using Microsoft.AspNetCore;
2-
using Microsoft.AspNetCore.Hosting;
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
33

44
namespace ScaffoldingSample.Api
55
{
66
public class Program
77
{
88
public static void Main(string[] args)
99
{
10-
CreateWebHostBuilder(args).Build().Run();
10+
CreateHostBuilder(args).Build().Run();
1111
}
1212

13-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14-
WebHost.CreateDefaultBuilder(args)
15-
.UseStartup<Startup>();
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
1619
}
1720
}

sample/ScaffoldingSample.Api/ScaffoldingSample.Api.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.App" />
10-
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
1210
</ItemGroup>
1311

1412
<ItemGroup>

sample/ScaffoldingSample.Api/Startup.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3-
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.EntityFrameworkCore;
54
using Microsoft.Extensions.Configuration;
65
using Microsoft.Extensions.DependencyInjection;
7-
using ScaffoldingSample.Models;
6+
using Microsoft.Extensions.Hosting;
7+
using ScaffoldingSample.Contexts;
88

99
namespace ScaffoldingSample.Api
1010
{
@@ -20,7 +20,7 @@ public Startup(IConfiguration configuration)
2020
// This method gets called by the runtime. Use this method to add services to the container.
2121
public void ConfigureServices(IServiceCollection services)
2222
{
23-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
23+
services.AddControllers();
2424
services.AddDbContext<NorthwindSlimContext>(builder =>
2525
{
2626
var connectionString = Configuration.GetConnectionString(nameof(NorthwindSlimContext));
@@ -29,14 +29,23 @@ public void ConfigureServices(IServiceCollection services)
2929
}
3030

3131
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
32+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3333
{
3434
if (env.IsDevelopment())
3535
{
3636
app.UseDeveloperExceptionPage();
3737
}
3838

39-
app.UseMvc();
39+
app.UseHttpsRedirection();
40+
41+
app.UseRouting();
42+
43+
app.UseAuthorization();
44+
45+
app.UseEndpoints(endpoints =>
46+
{
47+
endpoints.MapControllers();
48+
});
4049
}
4150
}
4251
}

0 commit comments

Comments
 (0)