[NHibernate异常,如果关联属性的列名称相同,则构建SessionFactory时

问题描述 投票:1回答:1

我有主表和明细表。两个表中的“主键”列的名称相同: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"); });
    }
}

here所述,我知道如果将[MasterTable1].[Id]重命名为[MasterTable1].[MasterId]并相应地修改实体和映射,这将很好地工作。

但是,我无法更改基础数据库;这是我必须处理的旧数据库。

而且,这是多对一关系。一个主管将有多个详细信息条目。

在不更改数据库中的列名的情况下,有没有办法做到这一点?>

我有主表和明细表。两个表中的“主键”列的名称相同:Id。建立会话工厂时,出现以下异常:NHibernate.MappingException:无法...

nhibernate nhibernate-mapping
1个回答
3
投票

不要将集合映射(关联表中的期望列)与实体映射(所有者表中的期望列)混淆。 ManyToOne是实体映射,它期望来自[[owner

© www.soinside.com 2019 - 2024. All rights reserved.