2016-09-26 125 views
0

我用我分貝第一,並有特定關聯,它總是返回空值,使用:實體框架包括表空

var results = _ABPRepository.Query(x => x.ABPArrangementId == arrangementId) 
        .Include(x => x.tb_ARRArrangement) 
        .Include(x => x.tb_ARRArrangement.Supplier) 
        .Include(x => x.tb_ARRArrangement.Supplier.tb_SCNSicCodesNew) 
        .Include(x => x.tb_BBRBatteryBackBrands) 
        .Include(x => x.tb_ARRArrangement.Supplier.Country) 
        .Include(x => x.tb_ARRArrangement.Supplier.InvoiceCountry) 
        .Include(x => x.Country) 
        .Select().FirstOrDefault(); 

下面總是空:

x.tb_ARRArrangement.Supplier.tb_SCNSicCodesNew 

我已經驗證該SQL是正確的(下面簡要):

SELECT 
    /* ... */ 
    [Project1].[SCN_SIC_Code] AS [SCN_SIC_Code], 
    [Project1].[SCN_SIC_Description] AS [SCN_SIC_Description] 
    /* ... */ 
    FROM (SELECT 
     /* ... */ 
     [Extent4].[SCN_SIC_Code] AS [SCN_SIC_Code], 
     [Extent4].[SCN_SIC_Description] AS [SCN_SIC_Description], 
     /* ... */ 
     CASE WHEN ([Extent8].[BBRIDNumber] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1] 
     FROM  [dbo].[tb_ABPArrangementBatteryBackProducer] AS [Extent1] 
     INNER JOIN [dbo].[tb_ARRArrangement] AS [Extent2] ON [Extent1].[ABPArrangementId] = [Extent2].[ARRIDNumber] 
     LEFT OUTER JOIN [dbo].[tb_SUPSupplier] AS [Extent3] ON [Extent2].[ARRSupplierId] = [Extent3].[SUPKey] 
     LEFT OUTER JOIN (SELECT 
           [tb_SCNSicCodesNew].[SCN_SIC_Code] AS [SCN_SIC_Code], 
           [tb_SCNSicCodesNew].[SCN_SIC_Description] AS [SCN_SIC_Description] 
         FROM [dbo].[tb_SCNSicCodesNew] AS [tb_SCNSicCodesNew]) AS [Extent4] ON [Extent3].[SUPEaHwrSicCode2007] = [Extent4].[SCN_SIC_Code] 
     LEFT OUTER JOIN [dbo].[tb_COUCountry] AS [Extent5] ON [Extent3].[SUPCountry] = [Extent5].[COUIDNumber] 
     LEFT OUTER JOIN [dbo].[tb_COUCountry] AS [Extent6] ON [Extent3].[SUPInvoiceCountry] = [Extent6].[COUIDNumber] 
     INNER JOIN [dbo].[tb_COUCountry] AS [Extent7] ON [Extent1].[ABPCorrespondanceCountry] = [Extent7].[COUIDNumber] 
     LEFT OUTER JOIN [dbo].[tb_BBRBatteryBackBrands] AS [Extent8] ON [Extent1].[ABPArrangementId] = [Extent8].[BBRABPIDNumber] 
     WHERE [Extent1].[ABPArrangementId] = 123456 
    ) AS [Project1] 

返回的記錄包含了我所期望的數據,但它不能映射到t他 results.tb_ARRArrangement.Supplier.tb_SCNSicCodesNew財產。

所有其他包括按預期工作。

我已經試過:

  • 閱讀有關地方包括可能無法正常工作(分組等),但他們似乎 不適用於我的方案爲包括不會被忽略, 更多的映射似乎失敗。

  • 卸下從EDMX和所述關聯和表重新添加

  • 重命名,並重新使關聯和導航屬性

  • 清潔和重新構建

有不是表格之間的數據庫中的外鍵,但其他人也沒有,因此我沒有任何問題。

要檢查的東西的任何指針?

+0

如何爲[ABPArrangementId:

using System.Data.Entity.Core.Common.CommandTrees; using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; using System.Data.Entity.Core.Metadata.Edm; using System.Data.Entity.Infrastructure.Interception; using System.Linq; namespace DataRepository { public class StringTrimmerInterceptor : IDbCommandTreeInterceptor { public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new StringTrimmerQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } } private class StringTrimmerQueryVisitor : DefaultExpressionVisitor { private static readonly string[] _typesToTrim = { "nvarchar", "varchar", "char", "nchar" }; public override DbExpression Visit(DbNewInstanceExpression expression) { var arguments = expression.Arguments.Select(a => { var propertyArg = a as DbPropertyExpression; if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name)) { return EdmFunctions.Trim(a); } return a; }); return DbExpressionBuilder.New(expression.ResultType, arguments); } } } } 

然後,這是通過將低於在同一項目作爲上下文初始化:從上面的鏈接的代碼中加入一個攔截器修剪字符串修復該問題]在你的模式int或varchar中定義? – DaniDev

+0

@DaniDev這是一個int – SWa

+0

Gottcha!它似乎基於linq引擎如何解析它。只是想確定。不幸的是,我沒有答案。 – DaniDev

回答

0

這是實體框架與加入固定長度的字符串爲可變長度的問題,因爲這裏說明:https://romiller.com/2014/10/20/ef6-1workaround-trailing-blanks-issue-in-string-joins/

的問題是,SQL Server忽略比較 字符串,但.NET字符串函數時,尾部的空格(EF使用的)沒有。在SQL上 服務器「ABCD」和「ABCD」被認爲是相等的,但在.NET中(和 EF)它們不是。

這導致正確的數據被選中但未映射。

using System.Data.Entity; 

namespace FixedLengthDemo 
{ 
    public class MyConfiguration : DbConfiguration 
    { 
     public MyConfiguration() 
     { 
      AddInterceptor(new StringTrimmerInterceptor()); 
     } 
    } 
}