首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BLtoolkit关联

BLtoolkit关联
EN

Stack Overflow用户
提问于 2013-03-13 04:16:01
回答 1查看 899关注 0票数 0

我有这个实体:

代码语言:javascript
复制
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实体:

代码语言:javascript
复制
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; }

    }
}

但是当我试图得到这样的问题时:

代码语言:javascript
复制
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; 
                }
            }

我得到了一个例外:

代码语言:javascript
复制
Association key 'c_to' not found for type 'Entities.dbo.Question.

你知道问题出在哪里吗?

我知道表中的tbl_question是c_to列。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-13 12:40:46

ThisKey属性表示定义关联一侧的关键字段(以逗号分隔)。实体类字段,不是数据库表字段!在您的情况下,您必须:

代码语言:javascript
复制
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; }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15371251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档