Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented sample for TrackableEntities/EntityFrameworkCore.Scaffold… #217

Closed
wants to merge 1 commit into from
Closed
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
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;
}
}