2013-03-12 6 views
0

j'ai cette entité:BLtoolkit association

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; } 

    } 
} 

conduisant à une entité Band:

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; } 

    } 
} 

mais lorsque je tente d'obtenir des questions telles que:

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; 
       } 
      } 

Je suis arrivé cette exception :

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

une idée où le problème?

Je sais que dans le tableau tbl_question est la colonne c_to ..

grâce

Répondre

1

La propriété ThisKey représente les champs clés (séparés par des virgules) du côté où l'association est définie. Le champ de la classe d'entité, pas le champ de la table de base de données! Dans votre cas, vous devez:

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; }