当前位置: 首页 > 工具软件 > BLToolkit > 使用案例 >

BLtoolkit : association

赵元白
2023-12-01

i have this entity:

  1. namespace Entities.dbo
  2. {
  3. [TableName("tbl_question")]
  4. public class Question : AbstractEntity
  5. {
  6. [MapField("c_from")]
  7. [Association(CanBeNull = false, OtherKey = "id", ThisKey = "c_from")]
  8. public User From { get; set; }
  9. [MapField("c_to")]
  10. [Association(CanBeNull = false, OtherKey = "id", ThisKey = "c_to")]
  11. public Band To { get; set; }
  12. }
  13. }

leading to a Band entity :

  1. namespace Entities.dbo
  2. {
  3. [TableName("tbl_band")]
  4. public class Band : AbstractEntity
  5. {
  6. [MapField("name")]
  7. public string Name { get; set; }
  8. [MapField("frontman")]
  9. [Association(CanBeNull = false, ThisKey = "frontman", OtherKey = "id")]
  10. public User Frontman { get; set; }
  11. }
  12. }

but when I try to get questions like :

  1. public static List<Question> GetQuestions(Band band)
  2. {
  3. using (var db = new MyDbManager())
  4. {
  5. try
  6. {
  7. var l = db.GetTable<Question>().Where(x => x.To == band).ToList();
  8. return l;
  9. }catch(Exception e)
  10. {
  11. return null;
  12. }
  13. }

I got this exception:

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

any idea wheres the problem ?

I know that in the table tbl_question is column c_to..

thanks


Tags:  database    entity    bltoolkit 

by Memoizer

The ThisKey property represents key fields (comma delimited) on the side where the association is defined. The field of entity class, not database table field! In your case, you must:

  1. 1. Define field in the Question entity for ThisKey property:
  2. [MapField("c_to")]
  3. public int BandId { get; set; }
  4. 2. Define field in the Band entity for OtherKey property:
  5. [MapField("id")]
  6. public string BandId { get; set; }
  7. 3. Rewrite To property in the Question entity:
  8. [Association(CanBeNull = false, OtherKey = "BandId", ThisKey = "BandId")]
  9. public Band To { get; set; }
 类似资料:

相关阅读

相关文章

相关问答