-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anthony Sneed
committed
Jun 19, 2019
1 parent
fbe0574
commit 055f275
Showing
21 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
# Trackable Entities for EF Core Handlebars Templates | ||
|
||
Handlebars templates for EF Core scaffolding to generate trackable entities. See [TrackableEntities.Core](https://github.com/TrackableEntities/TrackableEntities.Core) and [EntityFrameworkCore.Scaffolding.Handlebars](https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars). | ||
|
||
## Usage | ||
|
||
1. Install **Trackable Entities for EF Core Handlebars Templates**. | ||
|
||
``` | ||
dotnet new -i TrackableEntities.Core.Templates | ||
``` | ||
2. Create a **.NET Core Class Library** project. | ||
3. Add Trackable Entities Handlebars templates from the project folder. | ||
``` | ||
dotnet new te-templates | ||
``` | ||
4. Add the following NuGet packages to the project. | ||
- Microsoft.EntityFrameworkCore.Design | ||
- Microsoft.EntityFrameworkCore.SqlServer | ||
- TrackableEntities.EF.Core | ||
- EntityFrameworkCore.Scaffolding.Handlebars | ||
5. Add a `ScaffoldingDesignTimeServices` class that implements `IDesignTimeServices` | ||
```csharp | ||
public class ScaffoldingDesignTimeServices : IDesignTimeServices | ||
{ | ||
public void ConfigureDesignTimeServices(IServiceCollection services) | ||
{ | ||
// Add Handlebars scaffolding templates | ||
services.AddHandlebarsScaffolding(); | ||
} | ||
} | ||
``` | ||
6. From the command-prompt execute the following: | ||
- Be sure to create the NorthwindSlim database in SQL Local DB, | ||
then run the script from bit.ly/northwindslim. | ||
``` | ||
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 | ||
``` | ||
20 changes: 20 additions & 0 deletions
20
sample/TrackableEntities.Core.Templates.Sample/CodeTemplates/CSharpDbContext/DbContext.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{> dbimports}} | ||
|
||
namespace {{namespace}} | ||
{ | ||
public partial class {{class}} : DbContext | ||
{ | ||
{{{> dbsets}}} | ||
{{#if entity-type-errors}} | ||
{{#each entity-type-errors}} | ||
{{spaces 8}}{{{entity-type-error}}} | ||
{{/each}} | ||
|
||
{{/if}} | ||
|
||
{{{> dbconstructor}}} | ||
|
||
{{{on-configuring}}} | ||
{{{on-model-creating}}} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...leEntities.Core.Templates.Sample/CodeTemplates/CSharpDbContext/Partials/DbConstructor.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{spaces 8}}public {{class}}(DbContextOptions<{{class}}> options) : base(options) | ||
{{spaces 8}}{ | ||
{{spaces 7}} } |
3 changes: 3 additions & 0 deletions
3
...ckableEntities.Core.Templates.Sample/CodeTemplates/CSharpDbContext/Partials/DbImports.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata; |
3 changes: 3 additions & 0 deletions
3
...TrackableEntities.Core.Templates.Sample/CodeTemplates/CSharpDbContext/Partials/DbSets.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#each dbsets}} | ||
{{spaces 8}}public virtual DbSet<{{set-property-type}}> {{set-property-name}} { get; set; } | ||
{{/each}} |
13 changes: 13 additions & 0 deletions
13
sample/TrackableEntities.Core.Templates.Sample/CodeTemplates/CSharpEntityType/Class.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{> imports}} | ||
|
||
namespace {{namespace}} | ||
{ | ||
{{#if class-annotation}} | ||
{{{class-annotation}}} | ||
{{/if}} | ||
public partial class {{class}} : ITrackable, IMergeable | ||
{ | ||
{{{> constructor}}} | ||
{{{> properties}}} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...bleEntities.Core.Templates.Sample/CodeTemplates/CSharpEntityType/Partials/Constructor.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{{#if lines}} | ||
{{spaces 8}}public {{class}}() | ||
{{spaces 8}}{ | ||
{{#each lines}} | ||
{{spaces 8}} {{property-name}} = new HashSet<{{property-type}}>(); | ||
{{/each}} | ||
{{spaces 7}} } | ||
|
||
{{/if}} |
10 changes: 10 additions & 0 deletions
10
...ackableEntities.Core.Templates.Sample/CodeTemplates/CSharpEntityType/Partials/Imports.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
{{#if use-data-annotations}} | ||
using System.ComponentModel.DataAnnotations; | ||
{{/if}} | ||
{{#each imports}} | ||
using {{import}}; | ||
{{/each}} | ||
using TrackableEntities.Common.Core; |
28 changes: 28 additions & 0 deletions
28
...ableEntities.Core.Templates.Sample/CodeTemplates/CSharpEntityType/Partials/Properties.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{{#each properties}} | ||
{{#each property-annotations}} | ||
{{spaces 8}}{{{property-annotation}}} | ||
{{/each}} | ||
{{spaces 8}}public {{property-type}} {{property-name}} { get; set; } | ||
{{/each}} | ||
{{#if nav-properties}} | ||
|
||
{{#each nav-properties}} | ||
{{#each nav-property-annotations}} | ||
{{spaces 8}}{{{nav-property-annotation}}} | ||
{{/each}} | ||
{{#if nav-property-collection}} | ||
{{spaces 8}}public virtual ICollection<{{nav-property-type}}> {{nav-property-name}} { get; set; } | ||
{{else}} | ||
{{spaces 8}}public virtual {{nav-property-type}} {{nav-property-name}} { get; set; } | ||
{{/if}} | ||
{{/each}} | ||
{{/if}} | ||
|
||
{{spaces 8}}[NotMapped] | ||
{{spaces 8}}public TrackingState TrackingState { get; set; } | ||
|
||
{{spaces 8}}[NotMapped] | ||
{{spaces 8}}public ICollection<string> ModifiedProperties { get; set; } | ||
|
||
{{spaces 8}}[NotMapped] | ||
{{spaces 8}}public Guid EntityIdentifier { get; set; } |
188 changes: 188 additions & 0 deletions
188
sample/TrackableEntities.Core.Templates.Sample/Contexts/NorthwindSlimContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
|
||
namespace TrackableEntities.Core.Templates.Sample.Models | ||
{ | ||
public partial class NorthwindSlimContext : DbContext | ||
{ | ||
public virtual DbSet<Category> Category { get; set; } | ||
public virtual DbSet<Customer> Customer { get; set; } | ||
public virtual DbSet<CustomerSetting> CustomerSetting { get; set; } | ||
public virtual DbSet<Employee> Employee { get; set; } | ||
public virtual DbSet<EmployeeTerritories> EmployeeTerritories { get; set; } | ||
public virtual DbSet<Order> Order { get; set; } | ||
public virtual DbSet<OrderDetail> OrderDetail { get; set; } | ||
public virtual DbSet<Product> Product { get; set; } | ||
public virtual DbSet<Territory> Territory { get; set; } | ||
|
||
public NorthwindSlimContext(DbContextOptions<NorthwindSlimContext> options) : base(options) | ||
{ | ||
} | ||
|
||
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.HasAnnotation("ProductVersion", "2.2.4-servicing-10062"); | ||
|
||
modelBuilder.Entity<Category>(entity => | ||
{ | ||
entity.Property(e => e.CategoryName) | ||
.IsRequired() | ||
.HasMaxLength(15); | ||
}); | ||
|
||
modelBuilder.Entity<Customer>(entity => | ||
{ | ||
entity.Property(e => e.CustomerId) | ||
.HasMaxLength(5) | ||
.ValueGeneratedNever(); | ||
|
||
entity.Property(e => e.City).HasMaxLength(15); | ||
|
||
entity.Property(e => e.CompanyName) | ||
.IsRequired() | ||
.HasMaxLength(40); | ||
|
||
entity.Property(e => e.ContactName).HasMaxLength(30); | ||
|
||
entity.Property(e => e.Country).HasMaxLength(15); | ||
}); | ||
|
||
modelBuilder.Entity<CustomerSetting>(entity => | ||
{ | ||
entity.HasKey(e => e.CustomerId) | ||
.HasName("PK_dbo.CustomerSetting"); | ||
|
||
entity.Property(e => e.CustomerId) | ||
.HasMaxLength(5) | ||
.ValueGeneratedNever(); | ||
|
||
entity.Property(e => e.Setting) | ||
.IsRequired() | ||
.HasMaxLength(50); | ||
|
||
entity.HasOne(d => d.Customer) | ||
.WithOne(p => p.CustomerSetting) | ||
.HasForeignKey<CustomerSetting>(d => d.CustomerId) | ||
.OnDelete(DeleteBehavior.ClientSetNull) | ||
.HasConstraintName("FK_CustomerSetting_Customer"); | ||
}); | ||
|
||
modelBuilder.Entity<Employee>(entity => | ||
{ | ||
entity.Property(e => e.BirthDate).HasColumnType("datetime"); | ||
|
||
entity.Property(e => e.City).HasMaxLength(15); | ||
|
||
entity.Property(e => e.Country).HasMaxLength(15); | ||
|
||
entity.Property(e => e.FirstName) | ||
.IsRequired() | ||
.HasMaxLength(20); | ||
|
||
entity.Property(e => e.HireDate).HasColumnType("datetime"); | ||
|
||
entity.Property(e => e.LastName) | ||
.IsRequired() | ||
.HasMaxLength(20); | ||
}); | ||
|
||
modelBuilder.Entity<EmployeeTerritories>(entity => | ||
{ | ||
entity.HasKey(e => new { e.EmployeeId, e.TerritoryId }) | ||
.HasName("PK_dbo.EmployeeTerritories"); | ||
|
||
entity.Property(e => e.TerritoryId).HasMaxLength(20); | ||
|
||
entity.HasOne(d => d.Employee) | ||
.WithMany(p => p.EmployeeTerritories) | ||
.HasForeignKey(d => d.EmployeeId) | ||
.HasConstraintName("FK_dbo.EmployeeTerritories_dbo.Employee_EmployeeId"); | ||
|
||
entity.HasOne(d => d.Territory) | ||
.WithMany(p => p.EmployeeTerritories) | ||
.HasForeignKey(d => d.TerritoryId) | ||
.HasConstraintName("FK_dbo.EmployeeTerritories_dbo.Territory_TerritoryId"); | ||
}); | ||
|
||
modelBuilder.Entity<Order>(entity => | ||
{ | ||
entity.Property(e => e.CustomerId).HasMaxLength(5); | ||
|
||
entity.Property(e => e.Freight) | ||
.HasColumnType("money") | ||
.HasDefaultValueSql("((0))"); | ||
|
||
entity.Property(e => e.OrderDate).HasColumnType("datetime"); | ||
|
||
entity.Property(e => e.ShippedDate).HasColumnType("datetime"); | ||
|
||
entity.HasOne(d => d.Customer) | ||
.WithMany(p => p.Order) | ||
.HasForeignKey(d => d.CustomerId) | ||
.HasConstraintName("FK_Orders_Customers"); | ||
}); | ||
|
||
modelBuilder.Entity<OrderDetail>(entity => | ||
{ | ||
entity.Property(e => e.Quantity).HasDefaultValueSql("((1))"); | ||
|
||
entity.Property(e => e.UnitPrice).HasColumnType("money"); | ||
|
||
entity.HasOne(d => d.Order) | ||
.WithMany(p => p.OrderDetail) | ||
.HasForeignKey(d => d.OrderId) | ||
.OnDelete(DeleteBehavior.ClientSetNull) | ||
.HasConstraintName("FK_Order_Details_Orders"); | ||
|
||
entity.HasOne(d => d.Product) | ||
.WithMany(p => p.OrderDetail) | ||
.HasForeignKey(d => d.ProductId) | ||
.OnDelete(DeleteBehavior.ClientSetNull) | ||
.HasConstraintName("FK_Order_Details_Products"); | ||
}); | ||
|
||
modelBuilder.Entity<Product>(entity => | ||
{ | ||
entity.Property(e => e.ProductName) | ||
.IsRequired() | ||
.HasMaxLength(40); | ||
|
||
entity.Property(e => e.RowVersion).IsRowVersion(); | ||
|
||
entity.Property(e => e.UnitPrice) | ||
.HasColumnType("money") | ||
.HasDefaultValueSql("((0))"); | ||
|
||
entity.HasOne(d => d.Category) | ||
.WithMany(p => p.Product) | ||
.HasForeignKey(d => d.CategoryId) | ||
.HasConstraintName("FK_Products_Categories"); | ||
}); | ||
|
||
modelBuilder.Entity<Territory>(entity => | ||
{ | ||
entity.Property(e => e.TerritoryId) | ||
.HasMaxLength(20) | ||
.ValueGeneratedNever(); | ||
|
||
entity.Property(e => e.TerritoryDescription) | ||
.IsRequired() | ||
.HasMaxLength(50); | ||
}); | ||
|
||
OnModelCreatingExt(modelBuilder); | ||
} | ||
|
||
partial void OnModelCreatingExt(ModelBuilder modelBuilder); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
sample/TrackableEntities.Core.Templates.Sample/Models/Category.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using TrackableEntities.Common.Core; | ||
|
||
namespace TrackableEntities.Core.Templates.Sample.Models | ||
{ | ||
public partial class Category : ITrackable, IMergeable | ||
{ | ||
public Category() | ||
{ | ||
Product = new HashSet<Product>(); | ||
} | ||
|
||
public int CategoryId { get; set; } | ||
public string CategoryName { get; set; } | ||
|
||
public virtual ICollection<Product> Product { get; set; } | ||
|
||
[NotMapped] | ||
public TrackingState TrackingState { get; set; } | ||
|
||
[NotMapped] | ||
public ICollection<string> ModifiedProperties { get; set; } | ||
|
||
[NotMapped] | ||
public Guid EntityIdentifier { get; set; } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
sample/TrackableEntities.Core.Templates.Sample/Models/Customer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using TrackableEntities.Common.Core; | ||
|
||
namespace TrackableEntities.Core.Templates.Sample.Models | ||
{ | ||
public partial class Customer : ITrackable, IMergeable | ||
{ | ||
public Customer() | ||
{ | ||
Order = new HashSet<Order>(); | ||
} | ||
|
||
public string CustomerId { get; set; } | ||
public string CompanyName { get; set; } | ||
public string ContactName { get; set; } | ||
public string City { get; set; } | ||
public string Country { get; set; } | ||
|
||
public virtual CustomerSetting CustomerSetting { get; set; } | ||
public virtual ICollection<Order> Order { get; set; } | ||
|
||
[NotMapped] | ||
public TrackingState TrackingState { get; set; } | ||
|
||
[NotMapped] | ||
public ICollection<string> ModifiedProperties { get; set; } | ||
|
||
[NotMapped] | ||
public Guid EntityIdentifier { get; set; } | ||
} | ||
} |
Oops, something went wrong.