Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

EF Core 2.1 Loading related Data #5276


User avatar
0
rvanwoezik created

Hi, I've extended User with HomeAddress, in EF core 2.0 i load the related entity like this:

var query = UserManager.Users
                .Include(e => e.HomeAddress)
                .Include(e => e.PostalAddress)
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    u =>
                        u.Name.Contains(input.Filter) ||
                        u.Surname.Contains(input.Filter) ||
                        u.UserName.Contains(input.Filter) ||
                        u.EmailAddress.Contains(input.Filter)
                );

Now in EF Core 2.1 my HomeAddress is empty. What am i forgetting? Thanks in advance


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    It looks like there is no problem. You can see what the generated SQL statement is, isn't there no data in the database? Can you share more code?

  • User Avatar
    0
    rvanwoezik created

    Same db, same code only EF Core is newer

  • User Avatar
    0
    alper created
    Support Team

    weird! how about declaring those properties in the DbContext

    modelBuilder.Entity<User>(u=>
    {     
        u.HasOne(e => e.HomeAddress)
            .HasForeignKey(e => e.HomeAddressId);
    
        u.HasOne(e => e.PostalAddress)
            .HasForeignKey(e => e.PostalAddressId);
    }
    
  • User Avatar
    0
    rvanwoezik created

    <cite>alper: </cite> weird! how about declaring those properties in the DbContext

    modelBuilder.Entity<User>(u=>
    {     
       u.HasOne(e => e.HomeAddress)
           .HasForeignKey(e => e.HomeAddressId);
    
       u.HasOne(e => e.PostalAddress)
           .HasForeignKey(e => e.PostalAddressId);
    }
    

    What i have now, which works in EF Core 2.0 is

    modelBuilder.Entity<User>(b =>
                {
                    b.OwnsOne(u => u.HomeAddress);
                    b.OwnsOne(u => u.PostalAddress);
                });
    

    Here is extension of user class:

    ...
      public virtual MaritalStatusType MaritalStatus { get; set; }
    
      public virtual PhysicalAddress HomeAddress { get; set; }
    
      public virtual PostalAddress PostalAddress { get; set; }
    

    And here is my PhysicalAddress class

    [Table("PhysicalAddresses")]
        public class PhysicalAddress : CreationAuditedEntity<long>
        {
            public const int MaxStreetNameLength = 64;
            public const int MaxStateOrProvinceLength = 2;
            public const int MaxPostCodeLength = 7;
            public const int MaxCityNameLength = 32;
    
            [Required]
            [MaxLength(MaxCityNameLength)]
            public virtual string City { get; set; }
    
            [Required]
            [MaxLength(MaxStreetNameLength)]
            public virtual string Street { get; set; }
    
            [Required]
            [MaxLength(MaxPostCodeLength)]
            public virtual string PostalCode { get; set; }
    
            public virtual string Number { get; set; }
    
            [MaxLength(MaxStateOrProvinceLength)]
            public virtual string StateOrProvince { get; set; }
    
            public virtual string Latitude { get; set; }
    
            public virtual string Longitude { get; set; }
    
            [NotMapped]
            public virtual string StreetAddress => (this.Street + ' ' + this.Number).TrimEnd();
    
            public PhysicalAddress()
            {
            }
    
            public PhysicalAddress(string city, string street, string number, string postalCode, string stateOrProvince)
            {
                City = city;
                Street = street;
                Number = number;
                PostalCode = postalCode;
                StateOrProvince = stateOrProvince;
            }
        }
    
  • User Avatar
    0
    maliming created
    Support Team

    hi rvanwoezik I saw that you are using OwnsOne why you don’t use a one-to-many relationship. Because you have split the table [Table("PhysicalAddresses")]

    Can you create a simple demo that reproduces the problem.

  • User Avatar
    0
    rvanwoezik created

    I've found it, with help from [https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities#mapping-owned-types-with-table-splitting])

    Removing CreationAuditedEntity<long> from the Owned class PhysicalAddress and PostalAddress is the fix.

  • User Avatar
    0
    maliming created
    Support Team

    :D