Efcore: Owned types of eager loaded entities with `.Include(...)` are not loaded

Created on 24 Aug 2017  路  4Comments  路  Source: dotnet/efcore

Owned types are supposed to be automatically eager loaded, but it doesn't happen when using .Include(...).

Steps to reproduce

Given this database model:
A DataSource has many Orders
An Order has an Address (owned type)

```c#
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<DataSource>(b =>
    {
        b.HasKey(e => new { e.Id });
        b.Property(e => e.Id).ValueGeneratedOnAdd();
    });

    builder.Entity<Order>(b =>
    {
        b.HasKey(e => new { e.Id });
        b.Property(e => e.Id).ValueGeneratedOnAdd();
        b.OwnsOne(x => x.Address);
        b.HasOne(e => e.DataSource).WithMany(e => e.Orders);
    });
}

}

public class DataSource
{
public int Id { get; set; }
public List Orders { get; set; }
}

public class Order
{
public int Id { get; set; }
public Address Address { get; set; }
public DataSource DataSource { get; set; }
}

public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}

Given this query:

```c#
var dataSource = datasource = dbContext
    .Set<DataSource>()
    .Include(x => x.Orders)
    .First();

Orders are eager loaded, but the Address property of all Orders will be null.

Workaround: Need to explicitly eager load the Address:
c# var dataSource = datasource = dbContext .Set<DataSource>() .Include(x => x.Orders) .ThenInclude(x => x.Address) .First();

Further technical details

EF Core version: 2.0.0-rtm-26452
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.3

closed-duplicate

Most helpful comment

duplicate #9210

All 4 comments

duplicate #9210

Hi @Costo. We are gathering information on the use of EF Core pre-release builds. You reported this issue shortly after the release of 2.0.0 RTM. It would be really helpful if you could let us know:

  • Did you consider testing your code against the pre-release builds?
  • Was there anything blocking you from using pre-release builds?
  • What do you think could make it easier for you to use pre-release builds in the future?

Thanks in advance for any feedback. Hopefully this will help us to increase the value of pre-release builds going forward.

Hi @ajcvickers.

Did you consider testing your code against the pre-release builds?

We started a new project just after the 2.0.0 RTM Release. As far as I know, this is still the latest version and there are no newer pre-release builds available on Nuget or here: https://github.com/aspnet/EntityFrameworkCore/releases.

Maybe you mean something else with "pre-release", like the MyGet feed? https://dotnet.myget.org/feed/aspnetcore-dev/package/nuget/Microsoft.EntityFrameworkCore.SqlServer

Anyway this was not a blocking issue and there was an easy workaround so we did not investigate further.

What do you think could make it easier for you to use pre-release builds in the future?

When an issue is fixed or a PR is merged, give info like "This is fixed in build 123456. Get the the latest binaries [here]"

@Costo Thanks for your reply. I was referring to the pre-release builds of 2.0.0 that shipped before the RTM, but since you started after 2.0.0 RTM that explains that. :-) Nevertheless we should do better at making it clear when builds are fixed in the nightlies and making people aware of that. Thanks for the feedback!

Was this page helpful?
0 / 5 - 0 ratings