Skip to content

Commit

Permalink
Implemented sample for TrackableEntities#216
Browse files Browse the repository at this point in the history
  • Loading branch information
Herdo committed Aug 10, 2022
1 parent 2b65b77 commit 9a9efe7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 19 deletions.
13 changes: 0 additions & 13 deletions sample/ScaffoldingSample/Contexts/NorthwindSlimContextPartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ public partial class NorthwindSlimContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
#pragma warning disable CS8604 // Possible null reference argument.
modelBuilder.Entity<dbo.Employee>()
.Property(e => e.Country)
.HasConversion(
v => v.ToString(),
v => (Country)Enum.Parse(typeof(Country?), v));

modelBuilder.Entity<dbo.Customer>()
.Property(e => e.Country)
.HasConversion(
v => v.ToString(),
v => (Country)Enum.Parse(typeof(Country?), v));
#pragma warning restore CS8604 // Possible null reference argument.
}
}
}
21 changes: 21 additions & 0 deletions sample/ScaffoldingSample/EmployeeId.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion sample/ScaffoldingSample/Models/dbo/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Order> Orders { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions sample/ScaffoldingSample/Models/dbo/Employee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public Employee()
Territories = new HashSet<Territory>();
}

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<Territory> Territories { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion sample/ScaffoldingSample/Models/dbo/Territory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public Territory()
Employees = new HashSet<Employee>();
}

public string TerritoryId { get; set; } = null!;
public ScaffoldingSample.TerritoryId TerritoryId { get; set; }
public string TerritoryDescription { get; set; } = null!;

public virtual ICollection<Employee> Employees { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
5 changes: 3 additions & 2 deletions sample/ScaffoldingSample/ScaffoldingSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
<PackageReference Include="EntityFrameworkCore.Scaffolding.Handlebars" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
21 changes: 21 additions & 0 deletions sample/ScaffoldingSample/TerritoryId.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9a9efe7

Please sign in to comment.