Entity framework 在EFCore中包含忽略的列
我有一个场景,我希望忽略一个列(这样它就不会在每个include上都加入),但如果需要的话,可以显式地包含它。对我来说,选择每个联接所需的所有其他列不是一个切实可行的解决方案。我将用户的个人资料图片存储在数据库中,所有者、midifier等几乎在每个对象上都加入,这使得一些查询非常缓慢 对于这种情况是否有最佳实践Entity framework 在EFCore中包含忽略的列,entity-framework,ef-core-3.1,Entity Framework,Ef Core 3.1,我有一个场景,我希望忽略一个列(这样它就不会在每个include上都加入),但如果需要的话,可以显式地包含它。对我来说,选择每个联接所需的所有其他列不是一个切实可行的解决方案。我将用户的个人资料图片存储在数据库中,所有者、midifier等几乎在每个对象上都加入,这使得一些查询非常缓慢 对于这种情况是否有最佳实践 编辑:我考虑创建第二个虚拟模型,映射到同一个表,其中只包含这一列,但映射到同一个表接缝的两个模型会产生一些问题。正如您在编辑部分中添加的那样。这就是我解决类似问题的方法。 这对我来说很
编辑:我考虑创建第二个虚拟模型,映射到同一个表,其中只包含这一列,但映射到同一个表接缝的两个模型会产生一些问题。正如您在编辑部分中添加的那样。这就是我解决类似问题的方法。 这对我来说很有效,并且今天已经投入生产。 当我需要的时候,我可以很容易地把尸体归档
public class Content
{
public Guid Id { get; set; }
public string Heading { get; set; }
public string Preamble { get; set; }
public virtual ContentBody Body { get; set; }
}
public class ContentBody
{
public string Body { get; set; }
public Guid Id { get; set; }
}
这是我进行模型映射的地方:
public class ContentMap : IEntityTypeConfiguration<Content>
{
public void Configure(EntityTypeBuilder<Content> builder)
{
// Primary Key
builder.ToTable("Content");
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).ValueGeneratedOnAdd();
builder.Property(p => p.Heading).HasColumnName("Heading");
builder.Property(p => p.Preamble).HasColumnName("Preamble");
// Relationships
builder.HasOne(t => t.Body)
.WithOne().HasForeignKey<ContentBody>(t => t.Id);
}
}
public class ContentBodyMap : IEntityTypeConfiguration<ContentBody>
{
public void Configure(EntityTypeBuilder<ContentBody> builder)
{
// Primary Key
builder.ToTable("Content");
builder.HasKey(e => e.Id);
builder.Property(p => p.Body).HasColumnName("Body");
}
}
公共类ContentMap:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
//主键
建造商。可转载(“内容”);
builder.HasKey(e=>e.Id);
属性(e=>e.Id).ValueGeneratedOnAdd();
builder.Property(p=>p.Heading).HasColumnName(“Heading”);
builder.Property(p=>p.Preamble).HasColumnName(“Preamble”);
//关系
builder.HasOne(t=>t.Body)
.WithOne().HasForeignKey(t=>t.Id);
}
}
公共类ContentBodyMap:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
//主键
建造商。可转载(“内容”);
builder.HasKey(e=>e.Id);
builder.Property(p=>p.Body).HasColumnName(“Body”);
}
}
非常感谢您。。。虽然EF不喜欢第二次使用同一张桌子?但我会尝试一下,如果它有效的话,我会把它标记为答案。您使用的是什么版本的EF?我使用的EF Core 3.1Feels有点粗糙,因为EF响应有点意外-例如,如果内容为空,关系对象为空?但是nvmd,就像一个魅力!非常感谢。