首页 新闻 搜索 专区 学院

EntityFramework 关联属性不延迟加载

0
悬赏园豆:5 [待解决问题]

直接显示null,默认或者开启了延迟加载都没用,导航属性也是virtual,为什么不延迟加载?

手工加载没问题,可以加载到关联的属性

全部完整代码如下

  class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (var efDbContext = new EfDbContext())
            {

                var customer = efDbContext.Customers.FirstOrDefault();

// efDbContext.Entry(customer).Collection(p => p.Orders).Load();

                var price = customer.Orders.FirstOrDefault().Price;

                efDbContext.SaveChanges();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        Console.ReadLine();
    }
}

class EfDbContext : DbContext
{
    public EfDbContext():base("name = ConnectionString")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EfDbContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Order>().HasRequired(o => o.Customer).WithMany(o => o.Orders)
            .HasForeignKey(o => o.CustomerId).WillCascadeOnDelete(false);
    }

    public IDbSet<Customer> Customers { get; set; }
    public IDbSet<Order> Orders { get; set; }
}

class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

class Order
{
    public int Id { get; set; }
    public double  Price { get; set; }
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }
}
问题补充:

把entity的class属性改成public,不报null错误了,但是在关联属性还没有使用的时候就提前加载了关联属性,这不是延迟加载吧?

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (var efDbContext = new EfDbContext())
            {

                var customer = efDbContext.Customers.FirstOrDefault();

                var price = customer.Orders.FirstOrDefault().Price;

                efDbContext.SaveChanges();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        Console.ReadLine();
    }
}

class EfDbContext : DbContext
{
    public EfDbContext():base("name = ConnectionString")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EfDbContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Order>().HasRequired(o => o.Customer).WithMany(o => o.Orders)
            .HasForeignKey(o => o.CustomerId).WillCascadeOnDelete(false);

        this.Configuration.ProxyCreationEnabled = true;
        this.Configuration.LazyLoadingEnabled = true;

        this.Configuration.AutoDetectChangesEnabled = true;
    }

    public IDbSet<Customer> Customers { get; set; }
    public IDbSet<Order> Orders { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public double  Price { get; set; }
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }
}
decharvard的主页 decharvard | 初学一级 | 园豆:85
提问于:2018-11-11 20:06
< >
分享
所有回答(2)
0

听过来人一句劝.
不要用延迟加载..这个东西就是一坨屎.

吴瑞祥 | 园豆:28851 (高人七级) | 2018-11-11 21:56
0

听过来人一句劝.
不要用延迟加载..这个东西就是一坨屎.

czd890 | 园豆:9531 (大侠五级) | 2018-11-12 10:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册