2

對文字牆請不要TLDR。 我有一個非常簡單的對象模型,基本上它是HasMany上的FNH映射覆蓋

public class Colony 
{ 
    public virtual IList<Ant> Ants { get; set; } 
} 

public class Ant 
{ 
    public bool Dead { get; set } 
    public virtual IList<Egg> Eggs { get; set; } 
} 

public class Egg 
{ 
    public bool Dead { get; set } 
    public virtual int IncubationPeriod { get; set; } 
} 

你的想法。所以我宣佈了兩個映射覆蓋。

public class ColonyMappingOverride : IAutoMappingOverride<Colony> 
{ 
    public void Override(AutoMapping<Colony> mapping) 
    { 
     mapping.HasMany(c => c.Ants).Where(x => !x.Dead); 
    } 
} 

public class AntMappingOverride : IAutoMappingOverride<Ant> 
{ 
    public void Override(AutoMapping<Ant> mapping) 
    { 
     mapping.HasMany(c => c.Eggs).Where(x => !x.Dead); 
    } 
} 

因此,當我從數據庫中獲取數據時,最終導致數據不一致。

例如:

Colony.Ants不包含任何死蟻(如預期),然而Colony.Ants [0] .Eggs包含所有雞蛋......死或沒有。

如果我打電話給Session.Refresh(Colony.Ants [0]),死蛋就會被刪除。

任何人都知道爲什麼延遲加載會忽略nts映射覆蓋的Where子句?

+0

做的結果,如果你刪除重寫的改變對我的作品? – Mert

+0

不,唯一發生的是當我執行session.refresh它不再刪除死蛋。 – Stewert

+0

你能確定你的覆蓋運行配置http://stackoverflow.com/questions/6413767/can-auto-mappings-conventions-work-with-mapping-overrides – Mert

回答

0

它使用FNH 2.0.1.0和NH 4.0.0.4000及以下固定碼

public class Colony : Entity 
{ 
    public Colony() 
    { 
     Ants = new List<Ant>(); 
    } 
    public virtual IList<Ant> Ants { get; set; } 
} 

public class Ant : Entity 
{ 
    public Ant() 
    { 
     Eggs = new List<Egg>(); 
    } 
    public virtual bool Dead { get; set; } 
    public virtual IList<Egg> Eggs { get; set; } 
} 

public class Egg : Entity 
{ 
    public virtual bool Dead { get; set; } 
    public virtual int IncubationPeriod { get; set; } 
} 

public class ColonyMappingOverride : IAutoMappingOverride<Colony> 
{ 
    public void Override(AutoMapping<Colony> mapping) 
    { 
     mapping.HasMany(c => c.Ants).Where(x => !x.Dead); 
    } 
} 

public class AntMappingOverride : IAutoMappingOverride<Ant> 
{ 
    public void Override(AutoMapping<Ant> mapping) 
    { 
     mapping.HasMany(c => c.Eggs).Where(x => !x.Dead); 
    } 
} 

代碼

var config = Fluently.Configure() 
    .Database(SQLiteConfiguration.Standard.InMemory().ShowSql().FormatSql()) 
    .Mappings(m => m 
     .AutoMappings.Add(AutoMap.AssemblyOf<Ant>() 
      .Conventions.Add(DefaultCascade.All()) 
      .Override<Colony>(new ColonyMappingOverride().Override) 
      .Override<Ant>(new AntMappingOverride().Override) 
     ) 
    ) 
    .BuildConfiguration(); 

using (var sf = config.BuildSessionFactory()) 
using (var session = sf.OpenSession()) 
{ 
    new SchemaExport(config).Execute(true, true, false, session.Connection, null); 

    using (var tx = session.BeginTransaction()) 
    { 
     session.Save(new Colony 
     { 
      Ants = 
      { 
       new Ant 
       { 
        Dead = true, 
        Eggs = 
        { 
         new Egg { Dead = true, IncubationPeriod = 1 }, 
         new Egg { Dead = false, IncubationPeriod = 2 }, 
        } 
       }, 
       new Ant 
       { 
        Dead = false, 
        Eggs = 
        { 
         new Egg { Dead = true, IncubationPeriod = 1 }, 
         new Egg { Dead = false, IncubationPeriod = 2 }, 
        } 
       }, 
      } 
     }); 

     tx.Commit(); 
    } 
    session.Clear(); 

    var result = session.QueryOver<Colony>() 
     .Fetch(c => c.Ants).Eager 
     .SingleOrDefault(); 

    Console.WriteLine(result.Ants[0].Eggs.All(e => !e.Dead)); 
}