Кажется, что ядро Entity Framework создает очень сложные запросы при настройке объектов значений с OwnsOne для одной и той же таблицы.
Entity: Restaurant.cs
public class Restaurant : Entity, IAggregateRoot
{
public RestaurantId Id { get; private set; }
public string Name { get; private set; }
public Address Address { get; private set; }
protected Restaurant()
{
}
public Restaurant SetAddress(double lat, double lng, string street, string location, string zip, string country)
{
Address = new Address(lat, lng, street, location, zip, country);
return this;
}
}
Объект значения: Address.cs
public class Address : ValueObject
{
public Point Location { get; set; }
public string Street { get; private set; }
public string City { get; private set; }
public string Country { get; private set; }
public string Zip { get; private set; }
private Address() { }
public Address(double lat, double lng, string street, string city, string zip, string country)
{
Location = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326).CreatePoint(new Coordinate(lng, lat));
Street = street;
City = city;
Country = country;
Zip = zip;
}
protected override IEnumerable<object> GetAtomicValues()
{
yield return Street;
yield return City;
yield return Country;
yield return Zip;
}
}
EF Config: RestaurantEntityTypeConfig.cs
internal class RestaurantEntityTypeConfig : IEntityTypeConfiguration<Restaurant>
{
public void Configure(EntityTypeBuilder<Restaurant> builder)
{
builder.ToTable("Restaurant", SchemaNames.Restaurant);
builder.HasKey(c => c.Id);
builder.OwnsOne(c => c.Address, c=>
{
c.WithOwner();
});
}
}
Метод репозитория:
public virtual async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, params public virtual async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
{
var query = _dbContext.Set<T>().Where(predicate);
query = includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
return await query.FirstOrDefaultAsync();
}
Запрос при выборе через репозиторий выглядит следующим образом:
SELECT TOP(1) [r].[Id], [r].[Name], [t].[Id], [t].[Address_City], [t].[Address_Country], [t].[Address_State], [t].[Address_Street], [t].[Address_ZipCode]
FROM [restaurant].[Restaurant] AS [r]
LEFT JOIN (
SELECT [r0].[Id], [r0].[Address_City], [r0].[Address_Country], [r0].[Address_State], [r0].[Address_Street], [r0].[Address_ZipCode], [r1].[Id] AS [Id0]
FROM [restaurant].[Restaurant] AS [r0]
INNER JOIN [restaurant].[Restaurant] AS [r1] ON [r0].[Id] = [r1].[Id]
WHERE [r0].[Address_ZipCode] IS NOT NULL OR ([r0].[Address_Street] IS NOT NULL OR ([r0].[Address_State] IS NOT NULL OR ([r0].[Address_Country] IS NOT NULL OR [r0].[Address_City] IS NOT NULL)))
) AS [t] ON [r].[Id] = [t].[Id]
WHERE [r].[Id] = @__p_0
Таблица sql выглядит следующим образом:
Мой ожидаемый результат - плоский запрос, поскольку объект значения сохраняется в той же таблице sql. Это ошибка структуры сущностей или мне не хватает конфигурации?