首页 > 解决方案 > 如果关联属性的列名相同,则在构建 SessionFactory 时出现 NHibernate 异常

问题描述

我有主表和明细表。两个表中主键列的名称相同:Id
在构建会话工厂时,我遇到了以下异常:

NHibernate.MappingException: Unable to build the insert statement for class ........Detail1Entity: a failure occured when adding the Id of the class ---> System.ArgumentException: The column 'Id' has already been added in this SQL builder
Parameter name: columnName
   at NHibernate.SqlCommand.SqlInsertBuilder.AddColumnWithValueOrType(String columnName, Object valueOrType)
   at NHibernate.SqlCommand.SqlInsertBuilder.AddColumns(String[] columnNames, Boolean[] insertable, IType propertyType)
   at NHibernate.Persister.Entity.AbstractEntityPersister.GenerateInsertString(Boolean identityInsert, Boolean[] includeProperty, Int32 j)
   --- End of inner exception stack trace ---
   at NHibernate.Persister.Entity.AbstractEntityPersister.GenerateInsertString(Boolean identityInsert, Boolean[] includeProperty, Int32 j)
   at NHibernate.Persister.Entity.AbstractEntityPersister.GenerateInsertString(Boolean[] includeProperty, Int32 j)
   at NHibernate.Persister.Entity.AbstractEntityPersister.PostInstantiate()
   at NHibernate.Persister.Entity.SingleTableEntityPersister.PostInstantiate()
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at ...........

我有以下表格:

CREATE Table MasterTable1
(
[Id] [INT] IDENTITY (1,1) NOT NULL CONSTRAINT MasterTable1PK PRIMARY KEY,
[MasterData] [VARCHAR] (100)
)

CREATE Table DetailTable1
(
[Id] [INT] IDENTITY (1,1) NOT NULL CONSTRAINT DetailTable1PK PRIMARY KEY,
[MasterId] [INT] NOT NULL CONSTRAINT DetailTable1_MasterTable1_FK FOREIGN KEY REFERENCES MasterTable(MasterId),
[DetailData] [VARCHAR] (100)
)

以下是实体定义:

public class Master1Entity
{
    public virtual int Id { get; set; }
    public virtual string MasterData { get; set; }
}

public class Detail1Entity
{
    public virtual int Id { get; set; }
    public virtual string DetailData { get; set; }

    public virtual Master1Entity Master1 { get; set; }
}

以下是映射:

internal class Master1Map : ClassMapping<Master1Entity>
{
    public Master1Map()
    {
        Table("MasterTable1");

        Id(x => x.Id, im =>
        {
            im.Column("Id");
            im.Generator(Generators.Identity);
        });
        Property(x => x.MasterData);
    }
}

internal class Detail1Map : ClassMapping<Detail1Entity>
{
    public Detail1Map()
    {
        Table("DetailTable1");

        Id(x => x.Id, im =>
        {
            im.Column("Id");
            im.Generator(Generators.Identity);
        });
        Property(x => x.DetailData);

        ManyToOne(x => x.Master1, map => { map.Column("Id"); });
    }
}

正如这里所解释的,我知道如果我重命名并相应[MasterTable1].[Id][MasterTable1].[MasterId]修改实体和映射,这将运作良好。

但是,我无法更改底层数据库;这是我必须承担的遗留数据库。

此外,这是多对一的关系。一个 Master 会有多个 Detail 条目。

有没有办法在不更改数据库中的列名的情况下完成这项工作?

标签: nhibernatenhibernate-mapping

解决方案


不要将集合映射(期望关联表中的列)与实体映射(期望所有者表中的列)混淆。 ManyToOne是实体映射,它需要来自所有者表的列。在您的情况下,您需要映射的Master1is和列的所有者表是:DetailTable1MasterId

internal class Detail1Map : ClassMapping<Detail1Entity>
{
...
 ManyToOne(x => x.Master1, map => { map.Column("MasterId"); });

推荐阅读