0

我有一個奇怪的問題,Linq有時會生成帶重複圖的實體。 使用包含查詢的請求不會生成重複項,並且在請求的圖的基礎上具有簡單限制。Linq使用與STE一起使用的實體會生成重複的圖形

當相同的查詢與圖中其他地方的另一個限制一起使用時,我有重複項。

考慮以下圖:

Entity1 
    Entity2 
    Entity3 
     Entity4 
     Entity5 
      Entity6 
     Entity7 
     Entity8 

現在,這個查詢:

var query = context.Entity1.Where(u => (u.StringProp.StartsWith(someValue))); 
query = query.Include(@"NavpropEntity2.NavPropEntity3.NavPropEntity4.NavPropEntity5.NavEntity6"); 
query = query.Include(@"NavpropEntity2.NavPropEntity3.NavPropEntity7"); 
query = query.Include(@"NavpropEntity2.NavPropEntity3.NavPropEntity8"); 

未在圖entity6產生重複。

但是,下面的查詢呢!

query = context.Entity1.Join(context.Entity3, u => u.Entity2.Entity3.Entity4Id, g => g.Id, (u, g) => new NotSoAnonymousTypeWithParameterLessConstructor { Entity4= g, Entity1 = u }) 
           .Where(
            ApplyNotSoAnonymousTypeRestrictions(
            StringProp, 
            Id) 
           ).Select(z => z.Entity1); 
    query = query.Include(@"Navprop1.NavProp2.NavProp3.NavProp4.NavProp5"); 
    query = query.Include(@"Navprop1.NavProp2.NavProp3.NavProp6"); 
    query = query.Include(@"Navprop1.NavProp2.NavProp3.NavProp7"); 

爲Entity6生成重複項。

請注意,實體圖在linq查詢的末尾有問題。 所以這是EF/STE問題而不是STE問題。

這是在圖形發回服務器以保持更改時發出的,如blog post中所述。

我希望有人遇到同樣的問題,並已找到解決方案。 TIA。

回答

0

使用EF5中引入的AsNoTracking()擴展時會出現問題。這與查詢結構無關。我的數據訪問代碼是從模板(.tt)生成的,今天我們遇到了同樣的問題,我可以比較一個沒有重複的方法和另一個方法。唯一的區別是。

相關問題