hphp
hphp

Reputation: 2272

EF Core Scaffold Custom Entities and DBContext

I'm using EF Core anf DB First Approach. I generate my dbcontext and entities class using dotnet ef dbcontext scaffold and it gave me expected result.

Then i need to add custom implementation in my dbcontext and entity class, but whenever i update my db, and re-scaffold it, all the files is replaced and all my custom implementation is gone also. How to scaffold both context and entity but with some custom configuration?

Here's example what I aim

I have BaseEntity.cs class

public class BaseEntity
{
    public int Id { get; set; }
    public bool Deleted { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedTime { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime? LastModifiedTime { get; set; }
}

and my entity will look like this

public partial class Education : BaseEntity
{
    public Education()
    {
        EducationDetail = new HashSet<EducationDetail>();
    }
    public int UserProfileId { get; set; }
    public int EnumEducationId { get; set; }
    public string Description { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    public virtual EnumEducation EnumEducation { get; set; }
    public virtual UserProfile UserProfile { get; set; }
    public virtual ICollection<EducationDetail> EducationDetail { get; set; }
}

But if i re-scaffold, it will become

public partial class Education 
{
    public Education()
    {
        EducationDetail = new HashSet<EducationDetail>();
    }
    public int Id { get; set; }
    public bool Deleted { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedTime { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime? LastModifiedTime { get; set; }
    public int UserProfileId { get; set; }
    public int EnumEducationId { get; set; }
    public string Description { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    public virtual EnumEducation EnumEducation { get; set; }
    public virtual UserProfile UserProfile { get; set; }
    public virtual ICollection<EducationDetail> EducationDetail { get; set; }
}

can i custom the scaffold? i found Entity Framework Core Customize Scaffolding, but i think it's no longer available / supported by microsoft

Any way around?

Upvotes: 2

Views: 1489

Answers (1)

bricelam
bricelam

Reputation: 30375

Option 1

Use partial classes. This works well when strictly adding to the classes. Unfortunately, it doesn't work well for your scenario since some of the generated properties would need to override the base members. But it would however work if you used an interface instead.

partial class Education : IEntity
{
}

Options 2

Use templates. This gives you complete control of the generated code. The EntityFrameworkCore.Scaffolding.Handlebars package enables templates via handlebars. The EFCore.TextTemplating sample shows how use T4 templates.

<#@ parameter name="EntityType" type="Microsoft.EntityFrameworkCore.Metadata.IEntityType" #>
<#@ parameter name="Code" type="Microsoft.EntityFrameworkCore.Design.ICSharpHelper" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
<#
    var baseProperties = new[]
    {
        "Id",
        "Deleted",
        "CreatedBy",
        "CreatedTime",
        "LastModifiedBy",
        "LastModifiedTime"
    };
#>
public partial class <#= EntityType.Name #> : BaseEntity
{
    <# foreach (var property in EntityType.GetProperties()
        .Where(p => !baseProperties.Contains(p.Name))) { #>
        
    public <#= Code.Reference(property.ClrType) #> <#= property.Name #> { get; set; }
    
    <# } #>
}

Upvotes: 2

Related Questions