Why inclusions are ignored in EF6

I have 2 objects.

The first entity:

 public WorkItem()
        {
            this.Usage = new Collection<ItemUsage>();
        }

        public int Id { get; set; }

        public string Description { get; set; }

        public virtual ICollection<ItemUsage> Usage { get; set; }

and the second object:

  public class ItemUsage
    {
        public ItemUsage()
        {
        }

        public int Id { get; set; }

        public int WorkItemId { get; set; }

        public string UsedFor { get; set; }

        public virtual WorkItem WorkItem { get; set; }
    }

But when I try to get all work items, the usage list is empty. Even if I add include.

 var all = EntitySet.Include(i => i.Usage).ToList();

False loading is disabled. Do you have any idea what is wrong?

If I check the query in dbset, there is no connection to the second table.

{SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Description] AS [Description]
    FROM [dbo].[WorkItem] AS [Extent1]
}

EDIT:
Configurations:

    entity.HasKey(f => f.Id);
    entity.Property(f => f.UsedFor).IsRequired().HasMaxLength(1000);

    entity.HasRequired(f => f.WorkItem).WithMany(s => s.Usage).HasForeignKey(s => s.WorkItemId).WillCascadeOnDelete();

and

    entity.HasKey(f => f.Id);
    entity.Property(f => f.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    entity.Property(f => f.Description).IsRequired().HasMaxLength(1000);

    entity.HasMany(s => s.Usage).WithRequired(s => s.WorkItem).HasForeignKey(s => s.WorkItemId).WillCascadeOnDelete();
+4
source share
1 answer
  • You do not check the request, pointing to DbSetduring debugging. To check which query is actually sent to the database, you should use the property Database.Log- Context Log Property

, .

  1. :

        this.Usage = new Collection<ItemUsage>();
    

    EF -, , . , . , .

:

class WorkItem
{
    private ICollection<ItemUsage> _usage;

    public virtual ICollection<ItemUsage> Usage
    {
        get { return _usage ?? (_usage = new Collection<ItemUsage>()); }
    }
}
+2

Source: https://habr.com/ru/post/1617046/


All Articles