【发布时间】: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