From 9a9efe74267dc41e3d0b5fc21ef5c374c52063bb Mon Sep 17 00:00:00 2001 From: Stefan Over Date: Wed, 10 Aug 2022 13:00:00 +0200 Subject: [PATCH] Implemented sample for TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars#216 --- .../Contexts/NorthwindSlimContextPartial.cs | 13 ------------ sample/ScaffoldingSample/EmployeeId.cs | 21 +++++++++++++++++++ .../ScaffoldingSample/Models/dbo/Customer.cs | 2 +- .../ScaffoldingSample/Models/dbo/Employee.cs | 4 ++-- .../ScaffoldingSample/Models/dbo/Territory.cs | 2 +- .../ScaffoldingDesignTimeServices.cs | 11 ++++++++++ .../ScaffoldingSample.csproj | 5 +++-- sample/ScaffoldingSample/TerritoryId.cs | 21 +++++++++++++++++++ 8 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 sample/ScaffoldingSample/EmployeeId.cs create mode 100644 sample/ScaffoldingSample/TerritoryId.cs diff --git a/sample/ScaffoldingSample/Contexts/NorthwindSlimContextPartial.cs b/sample/ScaffoldingSample/Contexts/NorthwindSlimContextPartial.cs index 9eb5b67..969d14d 100644 --- a/sample/ScaffoldingSample/Contexts/NorthwindSlimContextPartial.cs +++ b/sample/ScaffoldingSample/Contexts/NorthwindSlimContextPartial.cs @@ -9,19 +9,6 @@ public partial class NorthwindSlimContext { partial void OnModelCreatingPartial(ModelBuilder modelBuilder) { -#pragma warning disable CS8604 // Possible null reference argument. - modelBuilder.Entity() - .Property(e => e.Country) - .HasConversion( - v => v.ToString(), - v => (Country)Enum.Parse(typeof(Country?), v)); - - modelBuilder.Entity() - .Property(e => e.Country) - .HasConversion( - v => v.ToString(), - v => (Country)Enum.Parse(typeof(Country?), v)); -#pragma warning restore CS8604 // Possible null reference argument. } } } \ No newline at end of file diff --git a/sample/ScaffoldingSample/EmployeeId.cs b/sample/ScaffoldingSample/EmployeeId.cs new file mode 100644 index 0000000..7bef485 --- /dev/null +++ b/sample/ScaffoldingSample/EmployeeId.cs @@ -0,0 +1,21 @@ +namespace ScaffoldingSample; + +public struct EmployeeId +{ + private readonly System.Int32 _value; + + private EmployeeId(System.Int32 value) + { + _value = value; + } + + public static explicit operator EmployeeId(System.Int32 value) + { + return new EmployeeId(value); + } + + public static explicit operator System.Int32(EmployeeId value) + { + return value._value; + } +} \ No newline at end of file diff --git a/sample/ScaffoldingSample/Models/dbo/Customer.cs b/sample/ScaffoldingSample/Models/dbo/Customer.cs index e236fcf..3cff293 100644 --- a/sample/ScaffoldingSample/Models/dbo/Customer.cs +++ b/sample/ScaffoldingSample/Models/dbo/Customer.cs @@ -14,7 +14,7 @@ public Customer() public string CompanyName { get; set; } = null!; public string? ContactName { get; set; } public string? City { get; set; } - public Country? Country { get; set; } = null!; + public string? Country { get; set; } public virtual CustomerSetting CustomerSetting { get; set; } = null!; public virtual ICollection Orders { get; set; } diff --git a/sample/ScaffoldingSample/Models/dbo/Employee.cs b/sample/ScaffoldingSample/Models/dbo/Employee.cs index 965b8cc..e9d24b4 100644 --- a/sample/ScaffoldingSample/Models/dbo/Employee.cs +++ b/sample/ScaffoldingSample/Models/dbo/Employee.cs @@ -10,13 +10,13 @@ public Employee() Territories = new HashSet(); } - public int EmployeeId { get; set; } + public ScaffoldingSample.EmployeeId EmployeeId { get; set; } public string LastName { get; set; } = null!; public string FirstName { get; set; } = null!; public DateTime? BirthDate { get; set; } public DateTime? HireDate { get; set; } public string? City { get; set; } - public Country? Country { get; set; } = null!; + public string? Country { get; set; } public virtual ICollection Territories { get; set; } diff --git a/sample/ScaffoldingSample/Models/dbo/Territory.cs b/sample/ScaffoldingSample/Models/dbo/Territory.cs index ed29983..2fae26c 100644 --- a/sample/ScaffoldingSample/Models/dbo/Territory.cs +++ b/sample/ScaffoldingSample/Models/dbo/Territory.cs @@ -10,7 +10,7 @@ public Territory() Employees = new HashSet(); } - public string TerritoryId { get; set; } = null!; + public ScaffoldingSample.TerritoryId TerritoryId { get; set; } public string TerritoryDescription { get; set; } = null!; public virtual ICollection Employees { get; set; } diff --git a/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs b/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs index f40860f..fd40489 100644 --- a/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs +++ b/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs @@ -61,6 +61,17 @@ public void ConfigureDesignTimeServices(IServiceCollection services) ? new EntityPropertyInfo("Country?", p.PropertyName, false) : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable)); + // Add Handlebars transformer for strong typed primary key columns + services.AddHandlebarsTransformers(propertyTransformer: propertyInfo => + { + if (propertyInfo.PropertyName == "EmployeeId") + return new EntityPropertyInfo(typeof(EmployeeId).FullName, propertyInfo.PropertyName, propertyInfo.PropertyIsNullable); + if (propertyInfo.PropertyName == "TerritoryId") + return new EntityPropertyInfo(typeof(TerritoryId).FullName, propertyInfo.PropertyName, true); + + return new EntityPropertyInfo(propertyInfo.PropertyType, propertyInfo.PropertyName, propertyInfo.PropertyIsNullable); + }); + // Add Handlebars transformer for Id property //services.AddHandlebarsTransformers2( // propertyTransformer: (e, p) => diff --git a/sample/ScaffoldingSample/ScaffoldingSample.csproj b/sample/ScaffoldingSample/ScaffoldingSample.csproj index 6f44b62..0bc08fa 100644 --- a/sample/ScaffoldingSample/ScaffoldingSample.csproj +++ b/sample/ScaffoldingSample/ScaffoldingSample.csproj @@ -7,8 +7,9 @@ - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/sample/ScaffoldingSample/TerritoryId.cs b/sample/ScaffoldingSample/TerritoryId.cs new file mode 100644 index 0000000..3a725e9 --- /dev/null +++ b/sample/ScaffoldingSample/TerritoryId.cs @@ -0,0 +1,21 @@ +namespace ScaffoldingSample; + +public struct TerritoryId +{ + private readonly System.String _value; + + private TerritoryId(System.String value) + { + _value = value; + } + + public static explicit operator TerritoryId(System.String value) + { + return new TerritoryId(value); + } + + public static explicit operator System.String(TerritoryId value) + { + return value._value; + } +} \ No newline at end of file