Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Nhibernate一对一关系返回总是null_C#_Sql Server_Nhibernate - Fatal编程技术网

C# Nhibernate一对一关系返回总是null

C# Nhibernate一对一关系返回总是null,c#,sql-server,nhibernate,C#,Sql Server,Nhibernate,我有两张桌子 第一个包含小部件属性: SQL HBM 第二个包含每个属性的数据/详细信息: SQL HBM 基本上在第一个表中,每个WidgetID都有几行,PropertyID一对一地引用PropertyDetail表,因此每个WidgetProperty.PropertyID在PropertyDetail表中只有一行。 在管理器中,如果我通过widgetID(es:4)调用对象,WidgetProperty.propertydeail始终为空。有任何提示吗?一对一引用用于连接共享同一

我有两张桌子

第一个包含小部件属性: SQL

HBM

第二个包含每个属性的数据/详细信息: SQL

HBM

基本上在第一个表中,每个
WidgetID
都有几行,
PropertyID
一对一地引用
PropertyDetail
表,因此每个WidgetProperty.PropertyID在
PropertyDetail
表中只有一行。
在管理器中,如果我通过
widgetID
(es:4)调用对象,
WidgetProperty.propertydeail
始终为空。有任何提示吗?

一对一引用用于连接共享同一主键的记录,或者二级表具有引用父行的FK的记录。(使用property ref选项)在本例中,您希望父表上的列指向子行。这被配置为多对一关系

<many-to-one name="PropertyDetail" cascade="all" column="PropertyId" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  /> 

如果需要一对一,则需要将PropertyDetail表上的PropertyId替换为WidgetPropertyId。本质上,如果PropertyId值等于WidgetPropertyId,那么您将实际使用当前代码返回一行,这肯定不是您所期望的。也就是说,如果您的WidgetProperty/w ID为1234,PropertyId为5,如果有ID为1234的PropertyDetail,它将返回与小部件1234关联的位置,因为一对一的性质是查找匹配的PK

编辑:对上述内容进行了更正。如果需要一对一,可以反转FK关联,在您的情况下,将WidgetPropertyId添加到PropertyDetail并从WidgetProperty中删除PropertyId。从那里,您可以通过删除外键并将其替换为property ref,告知NH通过其FK列而不是PK链接PropertyDetail,从而设置从Widget到PropertyDetail的一对一:

<one-to-one name="PropertyDetail" cascade="all" property-ref="WidgetPropertyId" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  />  

public class WidgetProperty
{
    public virtual Int32 WidgetPropertyID { get; set; }
    public virtual Int32 WidgetID { get; set; }
    public virtual Int32 PropertyID { get; set; }
    public virtual Int32 PropertyIndex { get; set; }
    public virtual PropertyDetail PropertyDetail { get; set; }
}
CREATE TABLE [dbo].[PropertyDetail](
    [PropertyID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Length] [nvarchar](50) NOT NULL,
    [Type] [smallint] NOT NULL,
    [DefaultHtml] [nvarchar](600) NULL,
 CONSTRAINT [PK_PropertyDetail] PRIMARY KEY CLUSTERED 
(
    [PropertyID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PropertyDetail] ADD  CONSTRAINT [DF__tblProper__Prope__276EDEB3]  DEFAULT ((1)) FOR [Type]
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Nt.Engine.domain.PropertyDetail,Nt.Engine" table="PropertyDetail" lazy="true">
    <id name="PropertyID" column="PropertyID">
      <generator class="native" />
    </id>
    <property name="Name" column="Name" type="string" length="50" not-null="true" />
    <property name="Length" column="Length" type="string" length="50" not-null="true" />
    <property name="Type" column="Type" type="byte" not-null="true" />
    <property name="DefaultHtml" column="DefaultHtml" type="string" length="600" not-null="false" />
  </class>
</hibernate-mapping>
public class PropertyDetail
{
    public virtual Int32 PropertyID { get; set; }
    public virtual String Name { get; set; }
    public virtual String Length { get; set; }        
    public virtual byte Type { get; set; }
    public virtual String DefaultHtml { get; set; }
}
<many-to-one name="PropertyDetail" cascade="all" column="PropertyId" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  /> 
<one-to-one name="PropertyDetail" cascade="all" property-ref="WidgetPropertyId" class="Nt.Engine.domain.PropertyDetail,Nt.Engine"  />