dotnet / dotnet/efcore

EFCore 7 - SqlServer - How to use Aggregate types in where clause for which ToJson is defined

Open
#29,500 2 comments 1 reaction 0 assignees View on GitHub
area-json area-query customer-reported needs-design
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I am running a sample application where I am using ToJson() for aggregate type "Contact".
After creating and populating the tables, I want to run a SELECT query which fetches data based on the 'Contact' field of the table.
For this I am executing following LINQ-
```C#
ContactDetails ctDetails = new() { Address = new("2 Main St", "Chigley", "CH1 5ZH", "UK"), Phone = "01632 12346" };

var result = context.Authors
.Where(author => author.Contact == ctDetails)
.ToList();
```
Executing the above LINQ is throwing following exception.
**System.InvalidOperationException: 'No backing field could be found for property 'ContactDetails.AuthorId' and the property does not have a getter.'**

My question is, Does EFCore 7 supports aggregate types in the Where clause in the LINQ for which ToJson() is defined?
If yes, how does is support? What am I missing in my sample application.
If no, are there any future plans on supporting such LINQ queries?

### Sample application

```C#
using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace test
{
class Program
{
public class ContactDetails
{
public Address Address { get; set; } = null!;
public string? Phone { get; set; }
}

public class Address
{
public Address(string street, string city, string postcode, string country)
{
Street = street;
City = city;
Postcode = postcode;
Country = country;
}

public string Street { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
}

public class Author
{
public int Id { get; set; }
public string Name { get; set; }
public ContactDetails Contact { get; set; }
}

public class AuthorContext : DbContext
{
public DbSet Authors { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"").LogTo(Console.WriteLine);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().OwnsOne(
author => author.Contact, ownedNavigationBuilder =>
{
ownedNavigationBuilder.ToJson();
ownedNavigationBuilder.OwnsOne(contactDetails => contactDetails.Address);
});
}

public static void Main()
{
using (var context = new AuthorContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();

var author1 = new Author()
{
Name = "Maddy Montaquila",
Contact = new ContactDetails()
{
Phone = "01632 12345",
Address = new Address("1 Main St", "Camberwick Green", "CW1 5ZH", "UK")
}
};
var author2 = new Author()
{
Name = "Jeremy Likness",
Contact = new ContactDetails()
{
Phone = "01632 12346",
Address = new Address("2 Main St", "Chigley", "CH1 5ZH", "UK")
}
};
context.Authors.Add(author1);
context.Authors.Add(author2);
context.SaveChanges();

ContactDetails ctDetails = new() { Address = new("2 Main St", "Chigley", "CH1 5ZH", "UK"), Phone = "01632 12346" };

var result = context.Authors
.Where(author => author.Contact == ctDetails)
.ToList();
}
}
}
}

}

```

### Stacktrace

```
at Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.GetMemberInfo(Boolean forMaterialization, Boolean forSet)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrPropertyGetterFactory.Create(IPropertyBase property)
at Microsoft.EntityFrameworkCore.Metadata.RuntimePropertyBase.Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.GetGetter()
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at test.Program.AuthorContext.Main() in C:\Users\shivamg\source\repos\ConsoleApp50\ConsoleApp50\Program.cs:line 88
```

### Provider and version information

EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.2.6

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.