Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ Scaffold EF Core models using Handlebars templates.

## Prerequisites

- [Visual Studio 2019](https://www.visualstudio.com/downloads/) 16.3 or greater.
- The .[NET Core 3.0 SDK](https://www.microsoft.com/net/download/core).
- [Visual Studio 2019](https://www.visualstudio.com/downloads/) 16.4 or greater.
- The .[NET Core 3.1 SDK](https://www.microsoft.com/net/download/core).

## Database Setup

1. Use SQL Server Management Studio to connect to SQL Server
- The easiest is to use **LocalDb**, which is installed with Visual Studio.
Connect to: `(localdb)\MsSqlLocalDb`.
- Create a new database named **NorthwindSlim**.
- Download the data file from <http://bit.ly/northwindslim>.
- Download the data file from <http://bit.ly/northwind-slim>.
- Unzip **NorthwindSlim.sql** and run the script to create tables and populate them with data.

## Usage

1. Create a new **.NET Core** class library.
- If necessary, edit the csproj file to update the **TargetFramework** to 3.0.
- If necessary, edit the csproj file to update the **TargetFramework** to 3.1.

> **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.

Expand Down Expand Up @@ -253,7 +253,7 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices

- Install the global `dotnet ef` tool.
```
dotnet tool install --global dotnet-ef --version 3.0.0-*
dotnet tool install --global dotnet-ef --version 3.1.0-*
```
- Open a command prompt at the project root and execute:
```
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.0.101"
"version": "3.1.100"
}
}
2 changes: 1 addition & 1 deletion sample/ScaffoldingSample.Api/ScaffoldingSample.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
{{#if model-namespace}}
using {{model-namespace}};
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata; // Comment
using {{models-namespace}};
{{#if model-namespace}}
using {{model-namespace}};
{{/if}}
8 changes: 4 additions & 4 deletions sample/ScaffoldingSample/Sample.ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ Demonstrates how to reverse engineer an existing database using the EF Core tool

## Prerequisites

- [Visual Studio 2019](https://www.visualstudio.com/downloads/) 16.3 or greater.
- The .[NET Core 3.0 SDK](https://www.microsoft.com/net/download/core).
- [Visual Studio 2019](https://www.visualstudio.com/downloads/) 16.4 or greater.
- The .[NET Core 3.1 SDK](https://www.microsoft.com/net/download/core).

## Database Setup

- Use SQL Server Management Studio to connect to SQL Server
- The easiest is to use **LocalDb**, which is installed with Visual Studio.
Connect to: `(localdb)\MsSqlLocalDb`.
- Create a new database named **NorthwindSlim**.
- Download the data file from <http://bit.ly/northwindslim>.
- Download the data file from <http://bit.ly/northwind-slim>.
- Unzip **NorthwindSlim.sql** and run the script to create tables and populate them with data.

## Setup
Expand Down Expand Up @@ -156,7 +156,7 @@ public partial class NorthwindSlimContext

- Install the global `dotnet ef` tool.
```
dotnet tool install --global dotnet-ef --version 3.0.0-*
dotnet tool install --global dotnet-ef --version 3.1.0-*
```
- Open a command prompt at the **ScaffoldingSample** project root and execute:
```
Expand Down
7 changes: 5 additions & 2 deletions sample/ScaffoldingSample/ScaffoldingSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
{{#if model-namespace}}
using {{model-namespace}};
{{/if}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
{{#if model-namespace}}
using {{model-namespace}};
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>3.5.1</PackageVersion>
<PackageVersion>3.6.0</PackageVersion>
<Authors>Tony Sneed</Authors>
<Company>Tony Sneed</Company>
<Title>Entity Framework Core Scaffolding with Handlebars</Title>
Expand Down Expand Up @@ -56,7 +56,7 @@
<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="1.10.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class HbsCSharpDbContextGenerator : CSharpDbContextGenerator
private const string EntityLambdaIdentifier = "entity";
private const string Language = "CSharp";
private readonly IOptions<HandlebarsScaffoldingOptions> _options;
private string _modelNamespace;

/// <summary>
/// CSharp helper.
Expand Down Expand Up @@ -94,18 +95,21 @@ public HbsCSharpDbContextGenerator(
/// Generate the DbContext class.
/// </summary>
/// <param name="model">Metadata about the shape of entities, the relationships between them, and how they map to the database.</param>
/// <param name="namespace">DbContext namespace.</param>
/// <param name="contextName">Name of DbContext class.</param>
/// <param name="connectionString">Database connection string.</param>
/// <param name="contextNamespace">Context namespace.</param>
/// <param name="modelNamespace">Model namespace.</param>
/// <param name="useDataAnnotations">If false use fluent modeling API.</param>
/// <param name="suppressConnectionStringWarning">Suppress connection string warning.</param>
/// <returns>DbContext class.</returns>
public override string WriteCode(
IModel model, string @namespace, string contextName, string connectionString,
bool useDataAnnotations, bool suppressConnectionStringWarning)
public override string WriteCode(IModel model, string contextName, string connectionString,
string contextNamespace, string modelNamespace, bool useDataAnnotations, bool suppressConnectionStringWarning)
{
Check.NotNull(model, nameof(model));

if (!string.IsNullOrEmpty(modelNamespace) && string.CompareOrdinal(contextNamespace, modelNamespace) != 0)
_modelNamespace = modelNamespace;

TemplateData = new Dictionary<string, object>();

if (_options.Value.TemplateData != null)
Expand All @@ -116,7 +120,7 @@ public override string WriteCode(
}
}

TemplateData.Add("namespace", @namespace);
TemplateData.Add("namespace", contextNamespace);

GenerateClass(model, contextName, connectionString, useDataAnnotations, suppressConnectionStringWarning);

Expand All @@ -140,6 +144,7 @@ protected override void GenerateClass(
Check.NotNull(contextName, nameof(contextName));
Check.NotNull(connectionString, nameof(connectionString));

TemplateData.Add("model-namespace", _modelNamespace);
TemplateData.Add("class", contextName);

GenerateDbSets(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO
{
generatedCode = CSharpDbContextGenerator.WriteCode(
model,
options.ContextNamespace ?? options.ModelNamespace,
options.ContextName,
options.ConnectionString,
options.ConnectionString,
options.ContextNamespace,
options.ModelNamespace,
options.UseDataAnnotations,
options.SuppressConnectionStringWarning);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO
{
generatedCode = CSharpDbContextGenerator.WriteCode(
model,
options.ContextNamespace ?? options.ModelNamespace,
options.ContextName,
options.ConnectionString,
options.ContextNamespace,
options.ModelNamespace,
options.UseDataAnnotations,
options.SuppressConnectionStringWarning);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ public class NullCSharpDbContextGenerator : ICSharpDbContextGenerator
/// Generate the DbContext class.
/// </summary>
/// <param name="model">Metadata about the shape of entities, the relationships between them, and how they map to the database.</param>
/// <param name="namespace">DbContext namespace.</param>
/// <param name="contextName">Name of DbContext class.</param>
/// <param name="connectionString">Database connection string.</param>
/// <param name="contextNamespace">Context namespace.</param>
/// <param name="modelNamespace">Model namespace.</param>
/// <param name="useDataAnnotations">If false use fluent modeling API.</param>
/// <param name="suppressConnectionStringWarning">True to suppress connection string warning.</param>
/// <returns>Generated DbContext class.</returns>
public string WriteCode(IModel model, string @namespace, string contextName, string connectionString, bool useDataAnnotations, bool suppressConnectionStringWarning)
/// <param name="suppressConnectionStringWarning">Suppress connection string warning.</param>
/// <returns>DbContext class.</returns>
public string WriteCode(IModel model, string contextName, string connectionString, string contextNamespace,
string modelNamespace, bool useDataAnnotations, bool suppressConnectionStringWarning)
{
return string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
{{#if model-namespace}}
using {{model-namespace}};
{{/if}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
{{#if model-namespace}}
using {{model-namespace}};
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public void WriteCode_Should_Generate_Context_File(bool useDataAnnotations)
connectionString: Constants.Connections.SqlServerConnection,
databaseOptions: new DatabaseModelFactoryOptions(),
modelOptions: new ModelReverseEngineerOptions(),
codeOptions: new ModelCodeGenerationOptions()
codeOptions: new ModelCodeGenerationOptions
{
ContextNamespace = Constants.Parameters.RootNamespace,
ModelNamespace = Constants.Parameters.RootNamespace,
ContextName = Constants.Parameters.ContextName,
ContextDir = Constants.Parameters.ProjectPath,
Expand Down Expand Up @@ -149,8 +150,9 @@ public void WriteCode_Should_Generate_Entity_Files(bool useDataAnnotations)
connectionString: Constants.Connections.SqlServerConnection,
databaseOptions: new DatabaseModelFactoryOptions(),
modelOptions: new ModelReverseEngineerOptions(),
codeOptions: new ModelCodeGenerationOptions()
codeOptions: new ModelCodeGenerationOptions
{
ContextNamespace = Constants.Parameters.RootNamespace,
ModelNamespace = Constants.Parameters.RootNamespace,
ContextName = Constants.Parameters.ContextName,
ContextDir = Constants.Parameters.ProjectPath,
Expand Down Expand Up @@ -188,8 +190,9 @@ public void WriteCode_Should_Generate_Context_and_Entity_Files(bool useDataAnnot
connectionString: Constants.Connections.SqlServerConnection,
databaseOptions: new DatabaseModelFactoryOptions(),
modelOptions: new ModelReverseEngineerOptions(),
codeOptions: new ModelCodeGenerationOptions()
codeOptions: new ModelCodeGenerationOptions
{
ContextNamespace = Constants.Parameters.RootNamespace,
ModelNamespace = Constants.Parameters.RootNamespace,
ContextName = Constants.Parameters.ContextName,
ContextDir = Constants.Parameters.ProjectPath,
Expand Down Expand Up @@ -229,8 +232,9 @@ public void Save_Should_Write_Context_File()
connectionString: Constants.Connections.SqlServerConnection,
databaseOptions: new DatabaseModelFactoryOptions(),
modelOptions: new ModelReverseEngineerOptions(),
codeOptions: new ModelCodeGenerationOptions()
codeOptions: new ModelCodeGenerationOptions
{
ContextNamespace = Constants.Parameters.RootNamespace,
ModelNamespace = Constants.Parameters.RootNamespace,
ContextName = Constants.Parameters.ContextName,
ContextDir = Path.Combine(directory.Path, "Contexts"),
Expand Down Expand Up @@ -264,8 +268,9 @@ public void Save_Should_Write_Entity_Files()
connectionString: Constants.Connections.SqlServerConnection,
databaseOptions: new DatabaseModelFactoryOptions(),
modelOptions: new ModelReverseEngineerOptions(),
codeOptions: new ModelCodeGenerationOptions()
codeOptions: new ModelCodeGenerationOptions
{
ContextNamespace = Constants.Parameters.RootNamespace,
ModelNamespace = Constants.Parameters.RootNamespace,
ContextName = Constants.Parameters.ContextName,
ContextDir = Path.Combine(directory.Path, "Contexts"),
Expand Down Expand Up @@ -299,8 +304,9 @@ public void Save_Should_Write_Context_and_Entity_Files()
connectionString: Constants.Connections.SqlServerConnection,
databaseOptions: new DatabaseModelFactoryOptions(),
modelOptions: new ModelReverseEngineerOptions(),
codeOptions: new ModelCodeGenerationOptions()
codeOptions: new ModelCodeGenerationOptions
{
ContextNamespace = Constants.Parameters.RootNamespace,
ModelNamespace = Constants.Parameters.RootNamespace,
ContextName = Constants.Parameters.ContextName,
ContextDir = Path.Combine(directory.Path, "Contexts"),
Expand Down
Loading