我試圖讓EF6流利映射爲我當前的項目工作。我有以下訂單映射:EF6 Fluent映射不正確的連接鍵
public class OrderMap : EntityTypeConfiguration<Order>
{
public OrderMap()
{
this.ToTable("OE_Orders");
this.Property(o => o.BillToAddress.Address1).HasColumnName("BillToAddress1");
this.Property(o => o.BillToAddress.Address2).HasColumnName("BillToAddress2");
this.Property(o => o.BillToAddress.City).HasColumnName("BillToCity");
this.Property(o => o.BillToAddress.State).HasColumnName("BillToState");
this.Property(o => o.BillToAddress.Zip).HasColumnName("BillToZip");
this.Property(o => o.ShipToAddress.Address1).HasColumnName("ShipToAddress1");
this.Property(o => o.ShipToAddress.Address2).HasColumnName("ShipToAddress2");
this.Property(o => o.ShipToAddress.City).HasColumnName("ShipToCity");
this.Property(o => o.ShipToAddress.State).HasColumnName("ShipToState");
this.Property(o => o.ShipToAddress.Zip).HasColumnName("ShipToZip");
this.Property(o => o.Stop).HasColumnName("RouteStopNumber");
this.HasKey(o => o.OrderNumber).HasMany(o => o.OrderLines).WithRequired(ol => ol.Order).HasForeignKey(ol => ol.OrderNumber);
this.HasKey(o => o.CustomerKey).HasRequired(o => o.Customer).WithRequiredDependent();
}
}
由於某種原因,這會嘗試將CustomerKey映射到OrderLines而不是OrderNumber。我不知道爲什麼。如果您需要它,我還可以包含OrderLineMap。生成的SQL是:
SELECT
[Project1].[CustomerKey] AS [CustomerKey],
[Project1].[OrderNumber] AS [OrderNumber],
[Project1].[OrderDate] AS [OrderDate],
[Project1].[InvoiceDate] AS [InvoiceDate],
[Project1].[DeliveryDate] AS [DeliveryDate],
[Project1].[ScheduledShipDate] AS [ScheduledShipDate],
[Project1].[Origination] AS [Origination],
[Project1].[RouteStopNumber] AS [RouteStopNumber],
[Project1].[RouteCode] AS [RouteCode],
[Project1].[BillToName] AS [BillToName],
[Project1].[BillToAddress1] AS [BillToAddress1],
[Project1].[BillToAddress2] AS [BillToAddress2],
[Project1].[BillToCity] AS [BillToCity],
[Project1].[BillToState] AS [BillToState],
[Project1].[BillToZip] AS [BillToZip],
[Project1].[ShipToName] AS [ShipToName],
[Project1].[ShipToAddress1] AS [ShipToAddress1],
[Project1].[ShipToAddress2] AS [ShipToAddress2],
[Project1].[ShipToCity] AS [ShipToCity],
[Project1].[ShipToState] AS [ShipToState],
[Project1].[ShipToZip] AS [ShipToZip],
[Project1].[EstimatedOrder] AS [EstimatedOrder],
[Project1].[TotalWeight] AS [TotalWeight],
[Project1].[CustomerKey1] AS [CustomerKey1],
[Project1].[CustomerCode] AS [CustomerCode],
[Project1].[Name] AS [Name],
[Project1].[Phone1] AS [Phone1],
[Project1].[Phone2] AS [Phone2],
[Project1].[FAX] AS [FAX],
[Project1].[Email] AS [Email],
[Project1].[Website] AS [Website],
[Project1].[Contact1] AS [Contact1],
[Project1].[Contact2] AS [Contact2],
[Project1].[Contact3] AS [Contact3],
[Project1].[Address1] AS [Address1],
[Project1].[Address2] AS [Address2],
[Project1].[City] AS [City],
[Project1].[State] AS [State],
[Project1].[Zip] AS [Zip],
[Project1].[C1] AS [C1],
[Project1].[OrderNumber1] AS [OrderNumber1],
[Project1].[LineType] AS [LineType],
[Project1].[LineItem] AS [LineItem],
[Project1].[TextFlag] AS [TextFlag],
[Project1].[ProductKey] AS [ProductKey],
[Project1].[UnitOfMeasure_SellBy] AS [UnitOfMeasure_SellBy],
[Project1].[Quantity_SellBy] AS [Quantity_SellBy],
[Project1].[UnitOfMeasure_Prc] AS [UnitOfMeasure_Prc],
[Project1].[UnitOfMeasure_Stk] AS [UnitOfMeasure_Stk],
[Project1].[UnitOfMeasure_Alt] AS [UnitOfMeasure_Alt],
[Project1].[OrderQuantity] AS [OrderQuantity],
[Project1].[Quantity_Stk] AS [Quantity_Stk],
[Project1].[Quantity_Alt] AS [Quantity_Alt],
[Project1].[Quantity_Prc] AS [Quantity_Prc],
[Project1].[PriceDefault] AS [PriceDefault],
[Project1].[PriceOverride] AS [PriceOverride],
[Project1].[PriceFlag] AS [PriceFlag]
FROM (SELECT
[Extent1].[CustomerKey] AS [CustomerKey],
[Extent1].[OrderNumber] AS [OrderNumber],
[Extent1].[OrderDate] AS [OrderDate],
[Extent1].[InvoiceDate] AS [InvoiceDate],
[Extent1].[DeliveryDate] AS [DeliveryDate],
[Extent1].[ScheduledShipDate] AS [ScheduledShipDate],
[Extent1].[Origination] AS [Origination],
[Extent1].[RouteStopNumber] AS [RouteStopNumber],
[Extent1].[RouteCode] AS [RouteCode],
[Extent1].[BillToName] AS [BillToName],
[Extent1].[BillToAddress1] AS [BillToAddress1],
[Extent1].[BillToAddress2] AS [BillToAddress2],
[Extent1].[BillToCity] AS [BillToCity],
[Extent1].[BillToState] AS [BillToState],
[Extent1].[BillToZip] AS [BillToZip],
[Extent1].[ShipToName] AS [ShipToName],
[Extent1].[ShipToAddress1] AS [ShipToAddress1],
[Extent1].[ShipToAddress2] AS [ShipToAddress2],
[Extent1].[ShipToCity] AS [ShipToCity],
[Extent1].[ShipToState] AS [ShipToState],
[Extent1].[ShipToZip] AS [ShipToZip],
[Extent1].[EstimatedOrder] AS [EstimatedOrder],
[Extent1].[TotalWeight] AS [TotalWeight],
[Extent2].[CustomerKey] AS [CustomerKey1],
[Extent2].[CustomerCode] AS [CustomerCode],
[Extent2].[Name] AS [Name],
[Extent2].[Phone1] AS [Phone1],
[Extent2].[Phone2] AS [Phone2],
[Extent2].[FAX] AS [FAX],
[Extent2].[Email] AS [Email],
[Extent2].[Website] AS [Website],
[Extent2].[Contact1] AS [Contact1],
[Extent2].[Contact2] AS [Contact2],
[Extent2].[Contact3] AS [Contact3],
[Extent2].[Address1] AS [Address1],
[Extent2].[Address2] AS [Address2],
[Extent2].[City] AS [City],
[Extent2].[State] AS [State],
[Extent2].[Zip] AS [Zip],
[Extent3].[OrderNumber] AS [OrderNumber1],
[Extent3].[LineType] AS [LineType],
[Extent3].[LineItem] AS [LineItem],
[Extent3].[TextFlag] AS [TextFlag],
[Extent3].[ProductKey] AS [ProductKey],
[Extent3].[UnitOfMeasure_SellBy] AS [UnitOfMeasure_SellBy],
[Extent3].[Quantity_SellBy] AS [Quantity_SellBy],
[Extent3].[UnitOfMeasure_Prc] AS [UnitOfMeasure_Prc],
[Extent3].[UnitOfMeasure_Stk] AS [UnitOfMeasure_Stk],
[Extent3].[UnitOfMeasure_Alt] AS [UnitOfMeasure_Alt],
[Extent3].[OrderQuantity] AS [OrderQuantity],
[Extent3].[Quantity_Stk] AS [Quantity_Stk],
[Extent3].[Quantity_Alt] AS [Quantity_Alt],
[Extent3].[Quantity_Prc] AS [Quantity_Prc],
[Extent3].[PriceDefault] AS [PriceDefault],
[Extent3].[PriceOverride] AS [PriceOverride],
[Extent3].[PriceFlag] AS [PriceFlag],
CASE WHEN ([Extent3].[OrderNumber] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[OE_Orders] AS [Extent1]
INNER JOIN [dbo].[AR_Customers] AS [Extent2] ON [Extent1].[CustomerKey] = [Extent2].[CustomerKey]
LEFT OUTER JOIN [dbo].[OE_OrderLines] AS [Extent3] ON [Extent1].[CustomerKey] = [Extent3].[OrderNumber]
WHERE [Extent1].[OrderNumber] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[CustomerKey] ASC, [Project1].[CustomerKey1] ASC, [Project1].[C1] ASC
這是有問題的行:
LEFT OUTER JOIN [dbo].[OE_OrderLines] AS [Extent3] ON [Extent1].[CustomerKey] = [Extent3].[OrderNumber]
不知道如何解決它。我可以使用一些建議或一些資源來幫助我弄清楚它爲什麼映射不正確。
讓我知道如何更新此信息以使其更具信息性。
謝謝大家的時間!
是'Order' - >'Customer'關係真的'一對-one'?很奇怪,通常一個'客戶'有很多'訂單',不是嗎? –
@IvanStoev是的,客戶有很多訂單,但我沒有將客戶與訂單集合進行映射。這太多了。我可以添加它,但這不是必需的。 – Carson
它不一定有一個集合。但'擁有'/'擁有'必須與關係基數相符。讓我以不同的方式問你 - 「Order.CustomerKey」是FK到「Customer」表嗎? –