2013-03-12 61 views
0

我有這樣的實體:BLtoolkit協會

namespace Entities.dbo 
{ 
    [TableName("tbl_question")] 
    public class Question : AbstractEntity 
    { 
     [MapField("c_from")] 
     [Association(CanBeNull = false, OtherKey = "id", ThisKey = "c_from")] 
     public User From { get; set; } 

     [MapField("c_to")] 
     [Association(CanBeNull = false, OtherKey = "id", ThisKey = "c_to")] 
     public Band To { get; set; } 

    } 
} 

導致樂隊實體:

namespace Entities.dbo 
{ 
    [TableName("tbl_band")] 
    public class Band : AbstractEntity 
    { 
     [MapField("name")] 
     public string Name { get; set; } 

     [MapField("frontman")] 
     [Association(CanBeNull = false, ThisKey = "frontman", OtherKey = "id")] 
     public User Frontman { get; set; } 

    } 
} 

,但是當我試圖讓這樣的問題:

public static List<Question> GetQuestions(Band band) 
     { 
      using (var db = new MyDbManager()) 
      { 
       try 
       { 

        var l = db.GetTable<Question>().Where(x => x.To == band).ToList(); 

        return l; 
       }catch(Exception e) 
       { 

        return null; 
       } 
      } 

我得到這個例外:

Association key 'c_to' not found for type 'Entities.dbo.Question. 

任何想法都會產生問題嗎?

我知道在表tbl_question是列c_to ..

感謝

回答

1

的ThisKey屬性表示在其中的關聯所定義的側關鍵字字段(逗號分隔)。實體類的字段,而不是數據庫表字段! 在你的情況下,你必須:

1. Define field in the Question entity for ThisKey property: 

[MapField("c_to")] 
public int BandId { get; set; } 

2. Define field in the Band entity for OtherKey property: 

[MapField("id")] 
public string BandId { get; set; } 

3. Rewrite To property in the Question entity: 

[Association(CanBeNull = false, OtherKey = "BandId", ThisKey = "BandId")] 
public Band To { get; set; }