プロジェクトの1つで構成に問題があり、SQL EFが生成しているのは、(少なくとも私にとっては)予期しないネストを示しているようです。この質問は少し時間がかかりますが、ご容赦ください。
だから、私は次のように設定しているとしましょう:
public class Order
{
public int Id { get; set; }
public decimal Amount { get; set; }
public Address ShippingAddress { get; set; }
}
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
そのように構成されたエンティティを使用して:
public class OrderConfiguration : EntityTypeConfiguration<Order>
{
public OrderConfiguration()
{
ToTable("Orders");
HasRequired(order => order.ShippingAddress)
.WithRequiredDependent()
.Map(mapping => mapping.MapKey("ShippingAddressId"));
}
}
public class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
ToTable("Persons");
HasRequired(person => person.Address)
.WithRequiredDependent()
.Map(mapping => mapping.MapKey("AddressId"));
}
}
私の目標は、住所が含まれている注文を取得することなので、次のような簡単なクエリを実行します。
var orders = context.Orders
.Include(order => order.ShippingAddress)
.Where(order => order.ShippingAddress.State == "FL")
.ToList();
SQLプロファイラーを監視しているときに、Ordersテーブルから選択してAddressテーブルで内部結合を実行するSQLが生成されることを期待しています。
SELECT * FROM [dbo].[Orders] AS [Extent1]
INNER JOIN [dbo].[Addresses] AS [Extent2] ON [Extent1].[AddressId] = [Extentd2].[Id]
WHERE [Extent2].[State] == 'FL'
代わりに、私はこれが生成されていることを発見しています:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Amount] AS [Amount],
[Join5].[Id1] AS [Id1],
[Join5].[StreetAddress] AS [StreetAddress],
[Join5].[City] AS [City],
[Join5].[State] AS [State],
[Join5].[ZipCode] AS [ZipCode],
[Join8].[Id2] AS [Id2],
[Join11].[Id3] AS [Id3]
FROM [dbo].[Orders] AS [Extent1]
INNER JOIN (SELECT [Extent2].[State] AS [State], [Extent3].[Id] AS [Id4]
FROM [dbo].[Addresses] AS [Extent2]
LEFT OUTER JOIN [dbo].[Orders] AS [Extent3] ON [Extent2].[Id] = [Extent3].[ShippingAddressId]
LEFT OUTER JOIN [dbo].[Persons] AS [Extent4] ON [Extent2].[Id] = [Extent4].[AddressId] ) AS [Join2] ON [Extent1].[Id] = [Join2].[Id4]
LEFT OUTER JOIN (SELECT [Extent5].[Id] AS [Id1], [Extent5].[StreetAddress] AS [StreetAddress], [Extent5].[City] AS [City], [Extent5].[State] AS [State], [Extent5].[ZipCode] AS [ZipCode], [Extent6].[Id] AS [Id5]
FROM [dbo].[Addresses] AS [Extent5]
LEFT OUTER JOIN [dbo].[Orders] AS [Extent6] ON [Extent5].[Id] = [Extent6].[ShippingAddressId]
LEFT OUTER JOIN [dbo].[Persons] AS [Extent7] ON [Extent5].[Id] = [Extent7].[AddressId] ) AS [Join5] ON [Extent1].[Id] = [Join5].[Id5]
LEFT OUTER JOIN (SELECT [Extent9].[Id] AS [Id2]
FROM [dbo].[Addresses] AS [Extent8]
LEFT OUTER JOIN [dbo].[Orders] AS [Extent9] ON [Extent8].[Id] = [Extent9].[ShippingAddressId]
LEFT OUTER JOIN [dbo].[Persons] AS [Extent10] ON [Extent8].[Id] = [Extent10].[AddressId] ) AS [Join8] ON [Extent1].[Id] = [Join8].[Id2]
LEFT OUTER JOIN (SELECT [Extent12].[Id] AS [Id6], [Extent13].[Id] AS [Id3]
FROM [dbo].[Addresses] AS [Extent11]
LEFT OUTER JOIN [dbo].[Orders] AS [Extent12] ON [Extent11].[Id] = [Extent12].[ShippingAddressId]
LEFT OUTER JOIN [dbo].[Persons] AS [Extent13] ON [Extent11].[Id] = [Extent13].[AddressId] ) AS [Join11] ON [Extent1].[Id] = [Join11].[Id6]
WHERE N'FL' = [Join2].[State]
明らかに、これは必要以上の作業です。これを行う簡単な方法はありますか、それともエンティティが正しく構成されていませんか?なぜこれらの余分な結合がすべて引き戻されるのか、またはそれらをその時点で必要なテーブルだけに制限する方法がわかりません。どんな洞察も大歓迎です!
ありがとう、デレク