我有一个数据库,其中许多表都有一组审计数据行:
public Guid CreatedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public Guid? ModifiedByUserId { get; set; }
public DateTime? ModifiedDate { get; set; }
例如,“区域”和“引用表”都有这样一组行。userIds通过一个外键链接到User表(如您所料)。
当我运行EF scaffolding生成器(这是一个db first项目)时,我运行以下代码:
dotnet ef dbcontext scaffold "....connection..." Microsoft.EntityFrameworkCore.SqlServer -o "output" --data-annotations
当我查看User类时,我得到了这样的结果:
public class User
{
public User()
{
AreaCreatedByUser = new HashSet<Area>();
AreaModifiedByUser = new HashSet<Area>();
CitationCreatedByUser = new HashSet<Citation>();
CitationModifiedByUser = new HashSet<Citation>();
}
public Guid Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
public Guid CreatedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public Guid? ModifiedByUserId { get; set; }
public DateTime? ModifiedDate { get; set; }
public virtual ICollection<Area> AreaCreatedByUser { get; set; }
[InverseProperty("ModifiedByUser")]
public virtual ICollection<Area> AreaModifiedByUser { get; set; }
[InverseProperty("CreatedByUser")]
public virtual ICollection<Citation> CitationCreatedByUser { get; set; }
[InverseProperty("ModifiedByUser")]
public virtual ICollection<Citation> CitationModifiedByUser { get; set; }
}
(它实际上在数百个表中使用,但为了更清楚起见,我将上面的内容缩写了一下。)
我真的不想在这些审计行中从一个用户导航到使用该用户的所有记录,但我不知道我可以做些什么来剥离它或防止它生成。当我从数据库中获得一个用户时,我不想要所有这些额外的字段,即使它们是null而没有包含。我猜如果我放弃FK关系,可能会这样做,但这似乎根本不是一个好主意。
有什么建议吗?
发布于 2019-02-14 09:33:49
目前的解决方案是在搭建后定制生成的代码。参见Customizing the Model。
您始终可以生成自己的模型,或者尝试使用第三方组件,如EntityFrameworkCore.Scaffolding.Handlebars
请注意,在手动构建或定制的模型中,EF Core中有一个非常酷的特性来处理这种“基础结构”列:Shadow Properties
https://stackoverflow.com/questions/54679707
复制相似问题