1

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

10
  • 1
    Try marking EntityB as virtual, so public virtual EntitiyB EntityB {get; set} Commented Jul 3, 2020 at 11:13
  • 2
    Please check the generated SQL to start with. Does it include the EntityB? Commented Jul 3, 2020 at 11:15
  • @MindSwipe I tried as you said but nothing has changed Commented Jul 3, 2020 at 11:18
  • @TomTom I had already looked at it but the result of the generated query is this: SELECT [p].[Id], [p].[IsConfirmed], [p].[EntityBId] FROM [EntityAs] AS [p] HERE [p].[IsConfirmed] = CAST(1 AS bit) Commented Jul 3, 2020 at 11:22
  • Ok, so EfCore is not generating the SQL.Your DbContext is NOT registering the metadata properly, so relationships are never parsed. Look up how to configure the DbContext properly. Commented Jul 3, 2020 at 11:32

1 Answer 1

1

When I tried to build a data model with the entities provided in question, I encountered a compilation error at Index attribute.

[Required] [Index(IsUnique = true)] public int EntityBId { get; set; } 

Turns out entity framework 6 libraries used to have that attribute, but the question talks about entity framework core. So I just removed the Index attribute and moved it to OnModelCreating method to mimic similar behavior.

protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<EntityA>().HasIndex(u => u.EntityBId).IsUnique(); } 

With that in place,I applied code first approach to generate the db and ran the program. Upon execution, ef core generated proper query:

SELECT [e].[Id], [e].[EntityBId], [e].[IsConfirmed], [e0].[Id], [e0].[Description], [e0].[Title] FROM [EntityAs] AS [e] INNER JOIN [EntityBs] AS [e0] ON [e].[EntityBId] = [e0].[Id] WHERE [e].[IsConfirmed] = CAST(1 AS bit) 

Hope it helps in resolving your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this solved my problem, my problem was due to the entity framework 6 library with entity framework core library

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.