Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading entity from DB does not properly load owned entities (In SQL Server Provider) #24165

Closed
J-Yen opened this issue Feb 16, 2021 · 2 comments

Comments

@J-Yen
Copy link

J-Yen commented Feb 16, 2021

Description
When loading an entity from the DB, some owned dependent types are not fetched correctly using the SQL Server Provider. It happens when there is an owned dependent that itself references one or more owned dependents, but has no other primitive properties with a value.

Expected behavior
Owned types should always be fetched when one or more nested properties have a value.

Code to reproduce

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;

namespace OwnedDependentNotIncludedInFetch
{
    class Program
    {
        static void Main(string[] args)
        {
            var aggregateRoot = new AnAggregateRoot
            {
                Id = Guid.NewGuid().ToString(),
                AProperty = "My aggregate root",
                OwnedDependent1 = new OwnedDependent1
                {
                    MyProperty = null, // When this is null the nested owned dependent is not included in fetch
                    OwnedDependent2 = new OwnedDependent2
                    {
                        MyProperty = "Nested owned dependent"
                    }
                }
            };

            using (var dbContext = new ExampleContext())
            {
                dbContext.Database.EnsureCreated();
                dbContext.AnAggregateRoots.Add(aggregateRoot);
                dbContext.SaveChanges();
            }

            using (var dbContext = new ExampleContext())
            {
                var fetchedAggregateRoot = dbContext.AnAggregateRoots
                    .Include(x => x.OwnedDependent1)
                    .SingleOrDefault(x => x.Id == aggregateRoot.Id);
                Console.WriteLine(JsonConvert.SerializeObject(fetchedAggregateRoot));
            }

            Console.ReadLine();
        }

        public class ExampleContext : DbContext
        {
            public DbSet<AnAggregateRoot> AnAggregateRoots { get; set; }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
                => optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true");
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<AnAggregateRoot>()
                    .HasKey(x => x.Id);
                modelBuilder.Entity<AnAggregateRoot>()
                    .Property(x => x.Id).ValueGeneratedNever();
                modelBuilder.Entity<AnAggregateRoot>()
                    .OwnsOne(x => x.OwnedDependent1, x => x.OwnsOne(y => y.OwnedDependent2));
            }
        }

        public class AnAggregateRoot
        {
            public string Id { get; set; }
            public string AProperty { get; set; }
            public OwnedDependent1 OwnedDependent1 { get; set; }
        }
        
        public class OwnedDependent1
        {
            public string MyProperty { get; set; }
            public OwnedDependent2 OwnedDependent2 { get; set; }
        }
        
        public class OwnedDependent2
        {
            public string MyProperty { get; set; }
        }
    }
}

Console output:

{
    "Id": "960c1a74-3bd7-4a42-a702-7c973c06fb19",
    "AProperty": "My aggregate root",
    "OwnedDependent1": null
}

Provider and version information
EF Core version:
EF Core version: 5.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 1909
IDE: Visual Studio 2019 16.7.6

This sample is working in EF Core 3.1.12 so it's a regression bug.

@ajcvickers
Copy link
Contributor

@smitpatel to de-dupe

@smitpatel
Copy link
Contributor

Duplicate of #23564

@smitpatel smitpatel marked this as a duplicate of #23564 Feb 16, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants