使用复合键进行一对一关系映射

时间:2010-08-09 08:20:55

标签: .net mapping castle-activerecord one-to-one

我有一个这样的数据库: 我的问题是“如何使用城堡活动记录映射此数据库?”

alt text http://cA4.upanh.com/11.396.15558077.LNS0/onetoonewithcompositekey.jpg 我试过这段代码: 运动:

[ActiveRecord("[Campaign]")]
    public class Campaign : ActiveRecordBase<Campaign>
    {
        private long m_ID;
        [PrimaryKey(Column = "`ID`")]
        public long ID
        {
            get { return m_ID; }
        set { m_ID = value; }
    }
        //// Properties ...
        private IList<MessageOfCampaign> m_MessageOfCampaign = new List<MessageOfCampaign>();
        [HasMany(typeof(MessageOfCampaign),
            Table = "`MessageOfCampaign`",            
            ColumnKey = "`CampaignID`")]
        public IList<MessageOfCampaign> MessagesOfCampaign
        {
            get { return m_MessageOfCampaign; }
            set { m_MessageOfCampaign = value; }
        }
    }

MessageOfCampaign:

[Serializable]
    public class MessageOfCampaignKey
    {
        private long m_MessageID;
        [KeyProperty(Column = "`MessageID`")]
        public long MessageID
        {
            get { return m_MessageID; }
            set { m_MessageID = value; }
        }

        private long m_CampaignID;
        [KeyProperty(Column = "`CampaignID`")]
        public long CampaignID
        {
            get { return m_CampaignID; }
            set { m_CampaignID = value; }
        }

        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }

            MessageOfCampaignKey key = obj as MessageOfCampaignKey;

            if (key == null)
            {
                return false;
            }
            if (m_CampaignID != key.m_CampaignID || !m_MessageID.Equals(key.m_MessageID))
            {
                return false;
            }
            return true;
        }

        public override int GetHashCode()
        {
            return this.m_CampaignID.GetHashCode() ^ this.m_MessageID.GetHashCode();
        }
    }

    [ActiveRecord("[MessageOfCampaign]")]
    public class MessageOfCampaign : ActiveRecordBase<MessageOfCampaign>
    {
        private MessageOfCampaignKey m_Key;
        [CompositeKey]
        public MessageOfCampaignKey Key
        {
            get { return m_Key; }
            set { m_Key = value; }
        }

        private Message m_Message;
        [OneToOne]
        public Message Message
        {
            get { return m_Message; }
            set { m_Message = value; }
        }

        private Campaign m_Campaign;
        [BelongsTo("`CampaignID`")]
        public Campaign Campaign
        {
            get { return m_Campaign; }
            set { m_Campaign = value; }
        }
    }

消息:

[ActiveRecord("[Message]")]
    public class Message : ActiveRecordBase
    {
        private long m_ID;
        [PrimaryKey(Column = "`ID`")]
        public long ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }
        //// Properties ...

        [OneToOne]
        public MessageOfCampaign MessageOfCampaign
        {
            get { return this.m_MessageOfCampaign; }
            set { this.m_MessageOfCampaign = value; }
        }
     }

但是我得到了错误:

  

断列映射:   MessageOfCampaign.id:   IMobileMarketing.Models.Message,输入   成分[邮件ID,的CampaignID]   需要2列,但是映射了1列

1 个答案:

答案 0 :(得分:1)

你的ER图似乎有问题。那不应该是两个1- *关系吗?然后,您可以使用HasAndBelongsToMany属性进行映射。 E.g。

[ActiveRecord("[Campaign]")]
public class Campaign : ActiveRecordBase<Campaign>
{
   private IList<Message> m_Messages;

    [HasAndBelongsToMany(typeof(Message), 
        Table="MessageOfCampaign", ColumnKey="CapaignId", ColumnRef="MessageId")]
    public IList<Message> Messages
    {
        get { return m_Messages; }
        set { m_Messages = value; }
    }

   ...
}

[ActiveRecord("[Message]")]
public class Message : ActiveRecordBase
{
       private IList<Capaign> m_Capaigns;

        [HasAndBelongsToMany(typeof(Capaign), 
            Table="MessageOfCampaign", ColumnKey="MessageId", ColumnRef="CapaignId")]
        public IList<Capaign> Capaigns
        {
            get { return m_Capaigns; }
            set { m_Capaigns = value; }
        }

   ...
}