Asp.net core EF Core Scaffold自定义实体和DBContext
我使用EF核心anf DB优先方法。我使用Asp.net core EF Core Scaffold自定义实体和DBContext,asp.net-core,entity-framework-core,ef-core-3.1,Asp.net Core,Entity Framework Core,Ef Core 3.1,我使用EF核心anf DB优先方法。我使用dotnet ef dbcontext scaffold生成了我的dbcontext和entities类,它给了我预期的结果 然后我需要在我的dbcontext和entity类中添加自定义实现,但每当我更新我的db并重新构建它时,所有文件都会被替换,所有自定义实现也都会消失。如何使用自定义配置构建上下文和实体 这是我的目标 我有BaseEntity.csclass public class BaseEntity { public int Id {
dotnet ef dbcontext scaffold
生成了我的dbcontext和entities类,它给了我预期的结果
然后我需要在我的dbcontext和entity类中添加自定义实现,但每当我更新我的db并重新构建它时,所有文件都会被替换,所有自定义实现也都会消失。如何使用自定义配置构建上下文和实体
这是我的目标
我有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; }
}
我的实体将是这样的
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; }
}
公共部分课程教育:BaseEntity
{
公共教育()
{
EducationDetail=新哈希集();
}
public int UserProfileId{get;set;}
公共int枚举教育ID{get;set;}
公共字符串说明{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间?结束日期{get;set;}
公共虚拟枚举教育枚举教育{get;set;}
公共虚拟用户配置文件用户配置文件{get;set;}
公共虚拟ICollection教育详细信息{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; }
}
公共部分班级教育
{
公共教育()
{
EducationDetail=新哈希集();
}
公共int Id{get;set;}
公共bool已删除{get;set;}
通过{get;set;}创建的公共字符串
公共日期时间CreatedTime{get;set;}
公共字符串LastModifiedBy{get;set;}
公共日期时间?LastModifiedTime{get;set;}
public int UserProfileId{get;set;}
公共int枚举教育ID{get;set;}
公共字符串说明{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间?结束日期{get;set;}
公共虚拟枚举教育枚举教育{get;set;}
公共虚拟用户配置文件用户配置文件{get;set;}
公共虚拟ICollection教育详细信息{get;set;}
}
我可以定制脚手架吗?我找到了,但我认为microsoft不再提供/支持它
有什么办法吗?选项1
使用。当严格地将添加到类中时,这种方法非常有效。不幸的是,它不适合您的场景,因为一些生成的属性需要覆盖基本成员。但是,如果您使用一个接口来代替它,它将起作用
部分班级教育:科学性
{
}
选择2
使用模板。这使您能够完全控制生成的代码。该软件包通过启用模板。示例显示了如何使用
公共部分类:BaseEntity
{
!baseProperties.Contains(p.Name)){
公共{get;set;}
}
<#@ 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; }
<# } #>
}