我的一个项目的配置遇到问题,并且生成的 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 Profiler 时,我希望看到生成的 SQL 从 Orders 表中选择并在 Address 表上进行内部连接
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]
显然,这比需要做的工作多得多。有没有更简单的方法可以做到这一点,还是我的实体配置不正确?我不明白为什么它要撤回所有这些额外的连接,或者我如何才能将它们限制在我当时需要的表中。任何见解将不胜感激!
谢谢,德里克