EF6 Fluent映射连接的错误键

时间:2016-07-19 16:29:31

标签: c# .net entity-framework orm ef-fluent-api

我正在尝试让EF6 Fluent Mappings为我当前的项目工作。我有以下订单映射:

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]

不确定如何修复它。我可以使用一些建议或一些资源来帮助我找出错误映射的原因。

让我知道如何更新此信息以使其更具信息性。

谢谢大家的时间!

1 个答案:

答案 0 :(得分:2)

问题在于配置的这两行:

// Print the submitted information:
if ( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['movie']) ) {
    echo "Thank you for completing the survey. Here's what you said";
    echo "<p>Your name is $_POST[name]";
    echo "<p>Your e-mail address is $_POST[email]";
    echo "<p>You recently watched $_POST[movie] and felt it was $_POST[rating]!";

    // add another if here
    if ($_POST['survey'] == 'yes') {
        echo "<p>Thank you for agreeing to be contacted for future surveys";
    } elseif ($_POST['survey'] == 'no') {
        echo "<p>We respect your with not to be contacted for future surveys";
    }

} else { // Missing form value.
    echo '<p>Please go back and fill out all required fields.</p>';
}

在EF Fluent API术语中, 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(); 表示映射主键,并且您已指定了两次。由于后者优先(覆盖先前的设置),因此EF将HasKey列映射为CustomerKey表的PK。

正确的配置应该是这样的(我建议你不要混合属性,PK和关联映射):

Order