How to delay (lazy) load a binary property byte[] in Entity Framework Core 6 - C#
The only way to optionally load something is to use navigation property.
The solution in your case is fake entity containing just the byte[]
property and configured with table splitting to share the same table with the primary entity.
Note that this is just logical separation and does not require DB schema changes. The very first line in the table splitting documentations states:
EF Core allows to map two or more entities to a single row. This is called table splitting or table sharing.
Probably you are confused with the term splitting. It's not splitting the physical table in the database, but splitting (sharing) it between several entities.
e.g.
Model
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; }
}
Configuration
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");
Now the Content.Data
won't be loaded automatically, and you can use the usual Include
to load it when needed. The only drawback is one additional object instance and actual byte[]
property accessor - content.Data.Data
vs the original content.Data
.