【问题标题】:make byte[] property load lazy使 byte[] 属性加载惰性
【发布时间】:2011-04-07 13:50:43
【问题描述】:

我正在使用 EF4 Code First,并且我有一个属性:

public byte[] Bytes {get;set;}

我可以让这个属性延迟加载吗(仅在需要时)?

【问题讨论】:

    标签: entity-framework lazy-loading ef-code-first entity-framework-4.1


    【解决方案1】:

    表拆分适用于 EF 4.1 RC:

    public class Item
    {
        public int Id { get; set; }
        ...
        public virtual ItemDetail ItemDetail { get; set; }
    }
    
    public class ItemDetail
    {
        public int Id { get; set; }
        public byte[] Bytes { get; set; }
    }
    
    public class Context : DbContext
    {
        public DbSet<Item> Items { get; set; }
        public DbSet<ItemDetail> ItemDetails { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Item>().ToTable("Items");
            modelBuilder.Entity<ItemDetail>().ToTable("Items");
            modelBuilder.Entity<Item>()
                .HasRequired(i => i.ItemDetail)
                .WithRequiredPrincipal();
        }
    }
    

    【讨论】:

      【解决方案2】:

      这确实是自 EF 1、EF 4 和 still in EF 4.1 以来的常见请求。

      该链接与 CTP5 相关,唯一可能的解决方案是 Table Splitting。您基本上需要定义两个实体类,但将它们映射到数据库中的一个表。然后,加载 byte[] 的任务被简化为加载正常的导航属性。

      帖子中的答案谈到了 CTP5 中的一个错误,它导致表格拆分无法正常工作,但希望现在在 EF 4.1 RC 中得到修复(但我不知道它是否真的修复了)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-19
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        • 2017-07-26
        • 2012-11-25
        • 2020-10-10
        • 1970-01-01
        相关资源
        最近更新 更多