开发者

How to map (and configure) a circular reference in NHibernate?

I have 2 tables that hold Office Locations and Computers: tblLocation and tblMachine respectively. Each of these tables reference the other. Each Location has multiple computers (machines). Each Location can send display information to any machine (regardless of location). One Machine could be the Display Machine of many locations.

I have the following setup using NHibernate as the ORM.

Simplified example of my tables:

  • tblLocation has columns LocationId (pk), LocationName, DisplayMachineId (fk)
  • tblMachine has columns MachineId (pk), MachineName, LocationId (fk)

I also have Location and Machine o开发者_JS百科bjects like this:

public class Location
{
    public virtual int LocationId { get; set; }
    public virtual string Name { get; set; }
    public virtual Machine DisplayMachine { get; set; }
    public virtual ISet<Machine> Machines { get; set; }
}

public class Machine
{
    public virtual int MachineId { get; set; }
    public virtual Location Location { get; set; }
    public virtual ISet<Location> Locations { get; set; }
}

Here are the mapping files for each:

<class name="DataTransfer.Machine,DataTransfer" table="tblMachine">
<id name="MachineId" column="MachineID" type="Int32" unsaved-value="0">
  <generator class="native"/>
</id>
<property name="Name" column="MachineName" type="string" not-null="true" />
<many-to-one name="Location" column="LocationID" not-null="false" class="DataTransfer.Location,DataTransfer" />
<set name="Locations" table="tblLocation" generic="true" inverse="true">
  <key column="DisplayFromMachineID" />
  <one-to-many class="DataTransfer.Location,DataTransfer"/>
</set>
<set name="MachineReportProperties" table="tblMachineReportProperty" generic="true" inverse="true">
  <key column="MachineID" />
  <one-to-many class="DataTransfer.MachineReportProperty,DataTransfer"/>
</set>
</class>

<class name="DataTransfer.Location,DataTransfer" table="tblLocation">
<id name="LocationId" column="LocationID" type="Int32" unsaved-value="0">
  <generator class="native"/>
</id>
<property name="Name" column="LocationName" type="string" not-null="true" />
<many-to-one name="DisplayMachine" column="DisplayFromMachineID" not-null="false" class="DataTransfer.Machine,DataTransfer" />
<set name="Machines" table="tblMachine" generic="true" inverse="true">
  <key column="LocationID" />
  <one-to-many class="DataTransfer.Machine,DataTransfer"/>
</set>
</class>

I am receiving a foreign key violation when attempting to query the database.

I have tried to start over and recreate the mappings and objects/properties, thinking I may have incorrectly configured them. Is there anything obvious that I have missed in this setup? I realize that ultimately the answer may be to change the database structure, I just wanted to know if there is anything I missed.

Note: when I remove the relation in the database from tblMachine.MachineId (pk) to tblLocation.DisplayMachineId the fk violation goes away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜