如何在 Entity Framework Core 6 中延迟(延迟)加载二进制 属性 byte[] - C#
How to delay (lazy) load a binary property byte[] in Entity Framework Core 6 - C#
我有简单的 table,其中包含密钥、名称和二进制内容。我只需要在需要时加载二进制内容。这曾经在 Linq2Sql 中非常简单,但在 EF core 6 中,除了导航集合的延迟加载之外,我找不到任何东西,这不是我需要的。是我遗漏了什么还是 EF 核心中遗漏了这个?
智日
public class Content {
public int Id { get; set; }
public string Name { get; set; }
public byte[] Data { get; set; } // How to delay loading this ?
}
命令
ctx.Content.Select(x =x.Id==1);
预期 SQL 是:
SELECT Id, Name FROM Content WHERE Id=1
可选加载内容的唯一方法是使用导航 属性。
您的解决方案是仅包含 byte[]
属性 并配置 table splitting 以与主要实体共享相同 table 的假实体。
请注意,这只是逻辑分离,不需要更改数据库架构。 table 拆分文档中的第一行指出:
EF Core allows to map two or more entities to a single row. This is called table splitting or table sharing.
您可能对拆分一词感到困惑。它不是在数据库中拆分物理 table,而是在多个实体之间拆分(共享)它。
例如
型号
public class Content
{
public int Id { get; set; }
public string Name { get; set; }
public ContentData Data { get; set; }
}
public class ContentData
{
public byte[] Data { get; set; }
}
配置
modelBuilder.Entity<ContentData>()
.ToTable("Content"); // must be the same as for Content entity
modelBuilder.Entity<ContentData>()
.Property<int>("ContentId");
modelBuilder.Entity<ContentData>()
.HasKey("ContentId");
modelBuilder.Entity<Content>()
.HasOne(e => e.ContentData)
.WithOne()
.HasForeignKey<ContentData>("ContentId");
现在Content.Data
不会自动加载,需要的时候可以用通常的Include
加载。唯一的缺点是一个额外的对象实例和实际的 byte[]
属性 访问器 - content.Data.Data
与原始 content.Data
.
我有简单的 table,其中包含密钥、名称和二进制内容。我只需要在需要时加载二进制内容。这曾经在 Linq2Sql 中非常简单,但在 EF core 6 中,除了导航集合的延迟加载之外,我找不到任何东西,这不是我需要的。是我遗漏了什么还是 EF 核心中遗漏了这个? 智日
public class Content {
public int Id { get; set; }
public string Name { get; set; }
public byte[] Data { get; set; } // How to delay loading this ?
}
命令
ctx.Content.Select(x =x.Id==1);
预期 SQL 是:
SELECT Id, Name FROM Content WHERE Id=1
可选加载内容的唯一方法是使用导航 属性。
您的解决方案是仅包含 byte[]
属性 并配置 table splitting 以与主要实体共享相同 table 的假实体。
请注意,这只是逻辑分离,不需要更改数据库架构。 table 拆分文档中的第一行指出:
EF Core allows to map two or more entities to a single row. This is called table splitting or table sharing.
您可能对拆分一词感到困惑。它不是在数据库中拆分物理 table,而是在多个实体之间拆分(共享)它。
例如
型号
public class Content
{
public int Id { get; set; }
public string Name { get; set; }
public ContentData Data { get; set; }
}
public class ContentData
{
public byte[] Data { get; set; }
}
配置
modelBuilder.Entity<ContentData>()
.ToTable("Content"); // must be the same as for Content entity
modelBuilder.Entity<ContentData>()
.Property<int>("ContentId");
modelBuilder.Entity<ContentData>()
.HasKey("ContentId");
modelBuilder.Entity<Content>()
.HasOne(e => e.ContentData)
.WithOne()
.HasForeignKey<ContentData>("ContentId");
现在Content.Data
不会自动加载,需要的时候可以用通常的Include
加载。唯一的缺点是一个额外的对象实例和实际的 byte[]
属性 访问器 - content.Data.Data
与原始 content.Data
.