0

我有这个实体:

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

    }
}

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

    }
}

但是当我尝试提出以下问题时:

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..

谢谢

4

1 回答 1

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; }
于 2013-03-13T04:40:45.850 回答