2012-03-13 99 views
0

轉介有很多線索。然而,這些實體通過代理標識符相關聯。在我的推薦實體中,我最終不得不添加一個映射到agent_id列的整數屬性,以使映射正常工作。流利-Nhibernate HasMany物件參考對象

如果我刪除從實體AGENTID屬性和「代理」對象,像這樣執行映射:

HasMany(x => x.Leads) 
     .AsBag() 
     .KeyColumn("Agent_Id") 
     .PropertyRef("Agent"); 

我碰到一個錯誤:

Object does not match target type.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.TargetException: Object does not match target type.

我猜,我問這是否是一個可接受的解決方案?額外的AgentID屬性將不會在屬性引用內的任何地方使用。是否有另一種方法可以在不必更改域模型的情況下執行此映射,因爲此時無法更改。

工作映射

public class Referral 
{ 
    public virtual int Id { get; set; } 
    public virtual int AgentID { get; set; } 
    public virtual Agent Agent { get; set; } 
    public virtual int? PositionNumber { get; set; } 
    public virtual DateTime? LastReferralDate { get; set; } 
    public virtual Account Account { get; set; } 

    public virtual IEnumerable<Lead> Leads { get; set; } 
} 

public ReferralMap() 
{ 
    Table("Referral"); 
    LazyLoad(); 
    Id(x => x.Id).GeneratedBy.Identity().Column("Id"); 
    Map(x => x.PositionNumber).Column("PositionNumber"); 
    Map(x => x.LastReferralDate).Column("LastReferralDate"); 
    Map(x => x.AgentID).Column("Agent_ID"); 
    References(x => x.Agent).Column("Agent_ID"); 
    References(x => x.Account).Column("Account_id"); 

    HasMany(x => x.Leads) 
     .AsBag() 
     .KeyColumn("Agent_Id") 
     .PropertyRef("AgentID"); 

} 

回答

1

我想這是因爲你有

public LeadMap() 
{ 
    Id(l => l.AgentId).Column("Agent_Id"); 
} 

代替

public LeadMap() 
{ 
    CompositeId().KeyReference(l => l.Agent, "Agent_Id"); 
} 
+0

感謝FIRO,而這是沒有問題的它指出我正確的方向。事實證明,使用公式映射更容易.Formula(「(select(count(*)from lead l where l.Agent_id = Agent_ID)」),而不是在不需要的東西上指定連接。 – Jesse 2012-03-18 16:19:40