[英]Nhibernate many to many with property
Hey i need help with a many to many relation in NHibernate i am getting this error :嘿,我需要帮助处理 NHibernate 中的多对多关系,我收到此错误:
NHibernate.MappingException: Unable to build the insert statement for class SelfHost.Core.Repository.implementation.HibernateProduktHaendler: a failure occured when adding the Id of the class ---> System.ArgumentException: The column 'Produkt_P_ID' has already been added in this SQL builder (Parameter 'columnName')
i have 3 tables produkt, Haendler and ProduktHaendler , the ProduktHaendler has Haendler_H_ID as a prime and foreign key at the same time and Produkt_P_ID as a prime and foreign key at the same time (i think this is the problem)我有 3 个表 produkt、Haendler 和 ProduktHaendler,ProduktHaendler 同时将 Haendler_H_ID 作为主键和外键,同时将 Produkt_P_ID 作为主键和外键(我认为这是问题所在)
this is my class Prdoukthaendler and the mapping这是我的班级 Prdoukthaendler 和映射
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelfHost.Core.Repository.implementation
{
public class HibernateProduktHaendler
{
public virtual HibernateProdukt Produkt_P_ID { set;get; }
public virtual string ProduktURL { set; get; }
public virtual HibernateHaendler Haendler_H_ID { set; get; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
HibernateProduktHaendler id;
id = (HibernateProduktHaendler)obj;
if (id == null)
return false;
if (Produkt_P_ID == id.Produkt_P_ID && Haendler_H_ID == id.Haendler_H_ID)
return true;
return false;
}
public override int GetHashCode()
{
return (Produkt_P_ID + "|" +Haendler_H_ID).GetHashCode();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="SelfHost"
namespace="SelfHost.Core.Repository.implementation">
<class name="HibernateProduktHaendler" table="ProduktHaendler">
<id class="HibernateProduktHaendler">
<key-property name="Produkt_P_ID" type="int"></key-property>
<key-property name="Haendler_H_ID" type="int"></key-property>
</id>
<property name="ProduktURL" />
<many-to-one name="Produkt_P_ID" ></many-to-one>
<many-to-one name="Haendler_H_ID" ></many-to-one>
</class>
</hibernate-mapping>
I believe what you are looking for is the composite-id mapping.我相信您正在寻找的是复合 ID映射。 You don't need the other many-to-one mappings.
您不需要其他多对一映射。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="SelfHost"
namespace="SelfHost.Core.Repository.implementation">
<class name="HibernateProduktHaendler" table="ProduktHaendler">
<composite-id>
<key-many-to-one name="Produkt_P_ID" />
<key-many-to-one name="Haendler_H_ID" />
</composite-id>
<property name="ProduktURL" />
</class>
</hibernate-mapping>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.