简体   繁体   English

没有键的NHibernate映射

[英]NHibernate mapping without keys

I have these two classes 我有这两节课

[DataServiceKey("Id")]
public class EmailMessage
{
    [DataMember]
    public virtual long Id { get; set; }

    [DataMember]
    public virtual string Scope { get; set; }

    [DataMember]
    public virtual long ScopeId { get; set; }

    ...

    [DataMember]
    public virtual IList<BinaryDataView> Attachments { get; set; }
}

and

public class BinaryDataView
{
    [DataMember]
    public virtual long BinaryId { get; set; }

    [DataMember]
    public virtual string ExternalLink { get; set; }
}

I am trying to create a propher nhibernate mapping for them but, data base are old enough and I can't use forenkeys. 我正在尝试为它们创建一个持久的nhibernate映射,但是数据库已经足够老了,不能使用forenkeys。 Both data bases (tEmail and tBinaryData) contains fields Scope and ScopeId and I need to use them to get BinaryData. 这两个数据库(tEmail和tBinaryData)都包含Scope和ScopeId字段,我需要使用它们来获取BinaryData。

So this is how my classes in hbm.xml looks like: 因此,这就是我在hbm.xml中的类的样子:

 <class name="EmailMessage" table="tEmail">
    <id name="Id">
      <column name="EmailId" sql-type="bigint"/>
      <generator class="native" />
    </id>
    <property name="DataOwnerId" />
    <property name="Scope"/>
    <property name="ScopeId"/>
    <property name="From" column="[From]"/>
    <property name="To" column="[To]"/>
    <property name="Subject"/>
    <property name="Body"/>
    <bag name="Attachments" lazy="false" fetch="join" generic="true">
      <key>
        <column name="Scope" />
        <column name="ScopeId" />
      </key>
      <one-to-many class="Entities.BinaryDataView,Entities" not-found="ignore"/>
    </bag>
  </class>

and

  <class name="BinaryDataView" table="tBinaryData" >
    <id name="BinaryId">
      <column name="BinaryId" sql-type="bigint"/>
      <generator class="native" />
    </id>
    <property name="ExternalLink"/>
  </class>

Right now I'm getting the: Foreign key (FK66731BD76FED2100:tBinaryData [Scope, ScopeId])) must have same number of columns as the referenced primary key (tEmail [EmailId]) error. 现在,我得到:外键(FK66731BD76FED2100:tBinaryData [Scope,ScopeId])必须具有与引用的主键(tEmail [EmailId])错误相同的列数。 So, there is my question: Where am I wrong and what I'm doing wrong? 所以,我有一个问题:我在哪里错了,我在做什么错? How can I create this mapping without using forenkeys for Scope and ScopeId? 如何在不为Scope和ScopeId使用forenkey的情况下创建此映射?

And sorry for my bad English. 抱歉我的英语不好。


Update: Managed to create mapping. 更新:托管创建映射。 At least right now its working: 至少现在它的工作是:

  <class name="EmailMessage" table="tEmail">
    <id name="Id">
      <column name="EmailId" sql-type="bigint"/>
      <generator class="native" />
    </id>
    <property name="DataOwnerId" />
    <property name="Scope" />
    <property name="ScopeId" />
    <properties name="ScopeInfo">
      <property name="Scope" column="Scope"/>
      <property name="ScopeId" column="ScopeId"/>
    </properties>
    <property name="From" column="[From]"/>
    <property name="To" column="[To]"/>
    <property name="Subject"/>
    <property name="Body"/>
    <bag name="Attachments" lazy="false" fetch="join" generic="true">
      <key property-ref="ScopeInfo" foreign-key="none">
        <column name="Scope" />
        <column name="ScopeId" />
      </key>
      <one-to-many class="Entities.BinaryDataView,Entities" not-found="ignore"/>
    </bag>
  </class>

The problem is EmailMessage exports two keys, scope and scopeId as attachments, but your BinaryDataView has only one ID BinaryID. 问题是EmailMessage会将两个密钥(scope和scopeId)导出为附件,但是BinaryDataView只有一个ID BinaryID。 The exported keys must match to ID of the otherside. 导出的密钥必须与另一侧的ID匹配。 The error message actually has nothing to do with ForeignKeys in the DB. 该错误消息实际上与数据库中的ForeignKeys无关。 Your mapping is wrong. 您的映射错误。 Consider adding a composite id to BinaryDataView , with scope and scopeid 考虑将具有范围和范围ID的复合ID添加到BinaryDataView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM