Question

i have this entity:

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

    }
}

leading to a Band entity :

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

    }
}

but when I try to get questions like :

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

I got this exception:

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

Was it helpful?

Solution

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. 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; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top