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: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ Before creating a pull request, please refer to the [Contributing Guidelines](ht
specific interfaces.
- When you run the _dotnet-ef-dbcontext-scaffold_ command again, you will see your updated reflected in the generated classes.

## Additional Partial Templates

You can add new partial templates to one of the `Partials` folders, then reference with the usual Handlebars syntax. For example, a file named **Comment.hbs** can be referenced from **Class.hbs**.

```hbs
{{{> comment}}}
public class {{class}}
{
```

## Excluded Tables

You can optionally exclude certain tables from code generation. These may also be qualified by schema name.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{> dbimports}}

namespace {{namespace}}
{ // Comment
public partial class {{class}} : DbContext
{
{{{> dbsets}}}
{{#if entity-type-errors}}
{{#each entity-type-errors}}
{{spaces 8}}{{{entity-type-error}}}
{{/each}}

{{/if}}

{{{> dbconstructor}}}

{{{> dbonconfiguring}}}

{{{on-model-creating}}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{spaces 8}}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{{spaces 8}}{
{{spaces 12}}if (!optionsBuilder.IsConfigured)
{{spaces 12}}{
{{#if suppress-connectionstring-warning}}
{{spaces 16}}{{sensitive-information-warning}}
{{/if}}
{{spaces 16}}optionsBuilder{{options-builder-provider}};
{{spaces 11}} }
{{spaces 7}} }
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

<ItemGroup>
<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>
<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 @@ -14,6 +14,8 @@ namespace {{namespace}}

{{{> dbconstructor}}}

{{{> dbonconfiguring}}}

{{{on-model-creating}}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{on-configuring}}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{> imports}}

namespace {{namespace}}
{ // Comment
{ {{{> comment}}}
{{#if class-annotation}}
{{spaces 4}}{{{class-annotation}}}
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Comment
15 changes: 14 additions & 1 deletion sample/ScaffoldingSample/Contexts/NorthwindSlimContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public NorthwindSlimContext(DbContextOptions<NorthwindSlimContext> options) : ba
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True");
}
}


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>(entity =>
Expand All @@ -37,6 +47,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<Customer>(entity =>
{
entity.HasComment("hello table Customer");

entity.Property(e => e.CustomerId)
.HasMaxLength(5)
.IsFixedLength();
Expand All @@ -45,7 +57,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

entity.Property(e => e.CompanyName)
.IsRequired()
.HasMaxLength(40);
.HasMaxLength(40)
.HasComment("hello CompanyName");

entity.Property(e => e.ContactName).HasMaxLength(30);

Expand Down
4 changes: 4 additions & 0 deletions sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using EntityFrameworkCore.Scaffolding.Handlebars;
using HandlebarsDotNet;
Expand All @@ -19,6 +20,9 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection services)
{
// Uncomment to launch JIT debugger and hit breakpoints
//Debugger.Launch();

// Add Handlebars scaffolding templates
services.AddHandlebarsScaffolding(options =>
{
Expand Down
5 changes: 1 addition & 4 deletions sample/ScaffoldingSample/ScaffoldingSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

<ItemGroup>
<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>
<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 @@ -12,7 +12,7 @@ public class CSharpTemplateLanguageService : ITemplateLanguageService
/// Get DbContext template file information.
/// </summary>
/// <returns>Dictionary of templates with file information.</returns>
public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo()
public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo(ITemplateFileService fileService)
{
var result = new Dictionary<string, TemplateFileInfo>
{
Expand All @@ -23,40 +23,18 @@ public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo()
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextDirectory,
FileName = Constants.DbContextTemplate + Constants.TemplateExtension
}
},
{
Constants.DbContextImportTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextImportTemplate + Constants.TemplateExtension
}
},
{
Constants.DbContextCtorTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextCtorTemplate + Constants.TemplateExtension
}
},
{
Constants.DbContextDbSetsTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
FileName = Constants.DbContextDbSetsTemplate + Constants.TemplateExtension
}
},
}
};

result = fileService.FindAllPartialTemplates(result, Constants.CSharpTemplateDirectories.DbContextPartialsDirectory);
return result;
}

/// <summary>
/// Get Entities template file information.
/// </summary>
/// <returns>Dictionary of templates with file information.</returns>
public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo()
public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo(ITemplateFileService fileService)
{
var result = new Dictionary<string, TemplateFileInfo>
{
Expand All @@ -68,31 +46,9 @@ public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo()
FileName = Constants.EntityTypeTemplate + Constants.TemplateExtension
}
},
{
Constants.EntityTypeImportTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
FileName = Constants.EntityTypeImportTemplate + Constants.TemplateExtension
}
},
{
Constants.EntityTypeCtorTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
FileName = Constants.EntityTypeCtorTemplate + Constants.TemplateExtension
}
},
{
Constants.EntityTypePropertyTemplate,
new TemplateFileInfo
{
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
FileName = Constants.EntityTypePropertyTemplate + Constants.TemplateExtension
}
},
};

result = fileService.FindAllPartialTemplates(result, Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory);
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<None Update="CodeTemplates\CSharpDbContext\Partials\DbConstructor.hbs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="CodeTemplates\CSharpDbContext\Partials\DbOnConfiguring.hbs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="CodeTemplates\CSharpDbContext\Partials\DbSets.hbs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand All @@ -98,6 +101,9 @@
<None Update="CodeTemplates\TypeScriptDbContext\Partials\DbConstructor.hbs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="CodeTemplates\TypeScriptDbContext\Partials\DbOnConfiguring.hbs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="CodeTemplates\TypeScriptDbContext\Partials\DbSets.hbs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
using EntityFrameworkCore.Scaffolding.Handlebars.Internal;

namespace EntityFrameworkCore.Scaffolding.Handlebars
Expand Down Expand Up @@ -28,6 +30,35 @@ public virtual string[] InputFiles(params InputFile[] files)
return filePaths.ToArray();
}

/// <summary>
/// Find all files in relative directory
/// </summary>
/// <param name="relativeDirectory">Relative Directory.</param>
/// <returns></returns>
public virtual string[] RetrieveAllFileNames(string relativeDirectory)
{
return Directory.GetFiles(relativeDirectory, $"*{Constants.TemplateExtension}").Select(x => Path.GetFileNameWithoutExtension(x)).ToArray();
}

/// <summary>
/// Finds all partial templates
/// </summary>
/// <param name="result">Dictionary containing template info</param>
/// <param name="relativeDirectory">Relative Directory.</param>
/// <returns></returns>
public virtual Dictionary<string, TemplateFileInfo> FindAllPartialTemplates(Dictionary<string, TemplateFileInfo> result, string relativeDirectory)
{
foreach (var file in RetrieveAllFileNames(relativeDirectory))
{
result.Add(file, new TemplateFileInfo()
{
RelativeDirectory = relativeDirectory,
FileName = file + Constants.TemplateExtension
});
}
return result;
}

/// <summary>
/// Retreive template file contents from the file system.
/// If template is not present, copy it locally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected override void GenerateOnConfiguring(string connectionString, bool supp
sb.AppendLine("}");
}

sb.AppendLine("}");
sb.AppendLine("}");
}

var onConfiguring = sb.ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
using Microsoft.EntityFrameworkCore.Design;
using HandlebarsLib = HandlebarsDotNet.Handlebars;
Expand All @@ -27,7 +26,7 @@ public class HbsDbContextTemplateService : HbsTemplateService, IDbContextTemplat
public HbsDbContextTemplateService(ITemplateFileService fileService,
ITemplateLanguageService languageService) : base(fileService, languageService)
{
DbContextTemplateFiles = LanguageService.GetDbContextTemplateFileInfo();
DbContextTemplateFiles = LanguageService.GetDbContextTemplateFileInfo(fileService);
}

/// <summary>
Expand Down Expand Up @@ -68,33 +67,14 @@ protected virtual Func<object, string> CompileDbContextTemplate(
protected override IDictionary<string, string> GetPartialTemplates(
LanguageOptions language = LanguageOptions.CSharp)
{
DbContextTemplateFiles.TryGetValue(Constants.DbContextImportTemplate, out TemplateFileInfo importFile);
var importTemplateFile = FileService.RetrieveTemplateFileContents(
importFile.RelativeDirectory, importFile.FileName);

DbContextTemplateFiles.TryGetValue(Constants.DbContextCtorTemplate, out TemplateFileInfo ctorFile);
var ctorTemplateFile = FileService.RetrieveTemplateFileContents(
ctorFile.RelativeDirectory, ctorFile.FileName);

DbContextTemplateFiles.TryGetValue(Constants.DbContextDbSetsTemplate, out TemplateFileInfo propertyFile);
var propertyTemplateFile = FileService.RetrieveTemplateFileContents(
propertyFile.RelativeDirectory, propertyFile.FileName);

var templates = new Dictionary<string, string>
var templates = new Dictionary<string, string>();
foreach(var item in DbContextTemplateFiles)
{
if(item.Value.RelativeDirectory == Constants.CSharpTemplateDirectories.DbContextPartialsDirectory)
{
Constants.DbContextImportTemplate.ToLower(CultureInfo.InvariantCulture),
importTemplateFile
},
{
Constants.DbContextCtorTemplate.ToLower(CultureInfo.InvariantCulture),
ctorTemplateFile
},
{
Constants.DbContextDbSetsTemplate.ToLower(CultureInfo.InvariantCulture),
propertyTemplateFile
},
};
templates.Add(item.Key, FileService.RetrieveTemplateFileContents(item.Value.RelativeDirectory, item.Value.FileName));
}
}
return templates;
}
}
Expand Down
Loading