【问题标题】:EF Core Scaffold Custom Entities and DBContextEF Core Scaffold 自定义实体和 DBContext
【发布时间】:2021-01-02 15:55:34
【问题描述】:

我正在使用 EF Core anf DB First Approach。我使用 dotnet ef dbcontext scaffold 生成了我的 dbcontext 和实体类,它给了我预期的结果。

然后我需要在我的 dbcontext 和实体类中添加自定义实现,但是每当我更新我的数据库并重新构建它时,所有文件都会被替换,我所有的自定义实现也都消失了。如何通过一些自定义配置同时构建上下文和实体?

这是我的目标示例

我有BaseEntity.cs 课程

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; }
}

我的实体看起来像这样

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; }
}

但如果我重新搭架,它会变成

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; }
}

我可以定制脚手架吗?我找到了Entity Framework Core Customize Scaffolding,但我认为它不再被微软提供/支持

有办法吗?

【问题讨论】:

    标签: asp.net-core entity-framework-core ef-core-3.1


    【解决方案1】:

    选项 1

    使用partial classes。这在严格添加类时效果很好。不幸的是,它不适用于您的方案,因为某些生成的属性需要override 基本成员。但是,如果您改用接口,它会起作用。

    partial class Education : IEntity
    {
    }
    

    选项 2

    使用模板。这使您可以完全控制生成的代码。 EntityFrameworkCore.Scaffolding.Handlebars 包通过handlebars 启用模板。 EFCore.TextTemplating 示例展示了如何使用 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; }
        
        <# } #>
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 1970-01-01
      • 2019-02-10
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多