I'm having a problem with the entity framework's "Include()" function
Framework used: .NET Core 3.1 with Entity Framework Core 3.1.5
These are my entities
public class EntityA { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] [Index(IsUnique = true)] public int EntityBId { get; set; } [ForeignKey("EntityBId")] public EntityB EntityB { get; set; } [DefaultValue(false)] public bool IsConfirmed { get; set; } } public class EntityB { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] public string Title { get; set; } public string Description { get; set; } } so I launch this query:
public ICollection<EntityA> GetAll(string contextName) { DbContext context = GetContext(contextName); ICollection<EntityA> collection = context.EntityAs .Include(j => j.EntityB) .Where(x => x.IsConfirmed) .ToList(); return collection; } private DbContext GetContext(string contextName) { string contextDbConnectionString = _secretsRepository.GetDbConnectionString(contextName); DbContextOptionsBuilder<DbContext> optionsBuilder = new DbContextOptionsBuilder<DbContext>(); optionsBuilder.UseSqlServer(contextDbConnectionString); return new DbContext(optionsBuilder.Options); } the result of the query is as follows:
{ Id: 5 IsConfirmed: true EntityB: null EntityBId: 72 } what I don't understand is why the "Include(j => j.EntityB)" does not return the EntityB field correctly valued
EntityBasvirtual, sopublic virtual EntitiyB EntityB {get; set}SELECT [p].[Id], [p].[IsConfirmed], [p].[EntityBId] FROM [EntityAs] AS [p] HERE [p].[IsConfirmed] = CAST(1 AS bit)