C# 延迟加载不加载ICollection属性

C# 延迟加载不加载ICollection属性,c#,entity-framework,C#,Entity Framework,通过延迟加载加载ICollection对象时遇到问题 public class Product { [Key] public int Id { get; set; } public string OrderNum { get; set; } public DateTime DateOfPurchased { get; set; } public int CustomerId { get; set; } public virtual ICollection<

通过延迟加载加载
ICollection
对象时遇到问题

public class Product
{
   [Key]
   public int Id { get; set; }
   public string OrderNum { get; set; }
   public DateTime DateOfPurchased { get; set; }
   public int CustomerId { get; set; }
   public virtual ICollection<OrderedItem> OrderedItems { get; set; }

   public virtual Customer Customer { get; set; }
 }

 public class OrderedItem
 {
    [Key]
    public int Id { get; set; }
    public int ProductId { get; set; }
    public int? Quantity { get; set; }
    public int? ItemId { get; set; }
    public decimal? TotalPrice { get; set; }
    public decimal? Profit { get; set; }

    public virtual Item Item { get; set; }
    public virtual Product Product { get; set; }
  }
公共类产品
{
[关键]
公共int Id{get;set;}
公共字符串OrderNum{get;set;}
public DateTime DateOfPurchased{get;set;}
public int CustomerId{get;set;}
公共虚拟ICollection OrderedItems{get;set;}
公共虚拟客户客户{get;set;}
}
公共类OrderedItem
{
[关键]
公共int Id{get;set;}
public int ProductId{get;set;}
公共整数?数量{get;set;}
公共int?ItemId{get;set;}
公共十进制?总价格{get;set;}
公共小数?利润{get;set;}
公共虚拟项项{get;set;}
公共虚拟产品产品{get;set;}
}

每当我将数据加载到
产品
,都可以加载除
ICollection
属性之外的所有属性。我犯了什么错误吗?

嗨,我在本地电脑上试过这个,效果很好。下面是模型类和dbContext类。我删除了一些属性以使其简单化。就在创建模型时,我添加了一行

EF类

        public class Product
            {
                [Key]
                public int Id { get; set; }
                public string OrderNum { get; set; }
                public DateTime DateOfPurchased { get; set; }
                public int CustomerId { get; set; }
                public virtual ICollection<OrderedItem> OrderedItems { get; set; }


            }

            public class OrderedItem
            {
                [Key]
                public int Id { get; set; }
                public int ProductId { get; set; }
                public int? Quantity { get; set; }
                public int? ItemId { get; set; }
                public decimal? TotalPrice { get; set; }
                public decimal? Profit { get; set; }

                public virtual Product Product { get; set; }
            }
公共类产品
{
[关键]
公共int Id{get;set;}
公共字符串OrderNum{get;set;}
public DateTime DateOfPurchased{get;set;}
public int CustomerId{get;set;}
公共虚拟ICollection OrderedItems{get;set;}
}
公共类OrderedItem
{
[关键]
公共int Id{get;set;}
public int ProductId{get;set;}
公共整数?数量{get;set;}
公共int?ItemId{get;set;}
公共十进制?总价格{get;set;}
公共小数?利润{get;set;}
公共虚拟产品产品{get;set;}
}
我的DBCOntext类:

     public class SampleDbContext : DbContext
    {
        public SampleDbContext()
            : base("name=SampleDBConnection")
        {
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ProxyCreationEnabled = true;
        }



        public DbSet<Product> Products { get; set; }

        public DbSet<OrderedItem> OrderedItems { get; set; }



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Product>().HasMany(c => c.OrderedItems);



        }
    }
公共类SampleDbContext:DbContext
{
公共SampleDbContext()
:base(“name=SampleDBConnection”)
{
this.Configuration.LazyLoadingEnabled=true;
this.Configuration.ProxyCreationEnabled=true;
}
公共数据库集产品{get;set;}
公共DbSet OrderedItems{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasMany(c=>c.OrderedItems);
}
}

输出:

嗨,我在本地电脑上试过这个,效果很好。下面是模型类和dbContext类。我删除了一些属性以使其简单化。就在创建模型时,我添加了一行

EF类

        public class Product
            {
                [Key]
                public int Id { get; set; }
                public string OrderNum { get; set; }
                public DateTime DateOfPurchased { get; set; }
                public int CustomerId { get; set; }
                public virtual ICollection<OrderedItem> OrderedItems { get; set; }


            }

            public class OrderedItem
            {
                [Key]
                public int Id { get; set; }
                public int ProductId { get; set; }
                public int? Quantity { get; set; }
                public int? ItemId { get; set; }
                public decimal? TotalPrice { get; set; }
                public decimal? Profit { get; set; }

                public virtual Product Product { get; set; }
            }
公共类产品
{
[关键]
公共int Id{get;set;}
公共字符串OrderNum{get;set;}
public DateTime DateOfPurchased{get;set;}
public int CustomerId{get;set;}
公共虚拟ICollection OrderedItems{get;set;}
}
公共类OrderedItem
{
[关键]
公共int Id{get;set;}
public int ProductId{get;set;}
公共整数?数量{get;set;}
公共int?ItemId{get;set;}
公共十进制?总价格{get;set;}
公共小数?利润{get;set;}
公共虚拟产品产品{get;set;}
}
我的DBCOntext类:

     public class SampleDbContext : DbContext
    {
        public SampleDbContext()
            : base("name=SampleDBConnection")
        {
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ProxyCreationEnabled = true;
        }



        public DbSet<Product> Products { get; set; }

        public DbSet<OrderedItem> OrderedItems { get; set; }



        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Product>().HasMany(c => c.OrderedItems);



        }
    }
公共类SampleDbContext:DbContext
{
公共SampleDbContext()
:base(“name=SampleDBConnection”)
{
this.Configuration.LazyLoadingEnabled=true;
this.Configuration.ProxyCreationEnabled=true;
}
公共数据库集产品{get;set;}
公共DbSet OrderedItems{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasMany(c=>c.OrderedItems);
}
}

输出:

迁移文件中是否定义了外键?@Béranger Yes。创建时定义了一个外键OrderedItem@hafizabdullah在尝试访问ICollection对象之前是否已释放了上下文?延迟加载规则:1 context.Configuration.ProxyCreationEnabled应为true。2:context.Configuration.LazyLoadingEnabled应为true。3:导航属性应定义为公共、虚拟。如果属性未定义为虚拟属性,则上下文不会进行延迟加载。@hafizabdullah请检查我的评论中提到的所有3个是否是迁移文件中定义的外键?@Béranger Yes。创建时定义了一个外键OrderedItem@hafizabdullah在尝试访问ICollection对象之前是否已释放了上下文?延迟加载规则:1 context.Configuration.ProxyCreationEnabled应为true。2:context.Configuration.LazyLoadingEnabled应为true。3:导航属性应定义为公共、虚拟。如果属性未定义为虚拟属性,则上下文不会进行延迟加载。@hafizabdullah请检查我的评论中提到的所有3个,以获得答案。用Fluent软件解决了这个问题API@hafizabdullah很高兴我希望你喜欢我的回答和解释。谢谢你的回答。用Fluent软件解决了这个问题API@hafizabdullah很高兴我希望你喜欢我的回答和解释。