nhibernate - 如果关联属性的列名相同,则在构建 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 条目。
有没有办法在不更改数据库中的列名的情况下完成这项工作?
解决方案
不要将集合映射(期望关联表中的列)与实体映射(期望所有者表中的列)混淆。 ManyToOne
是实体映射,它需要来自所有者表的列。在您的情况下,您需要映射的Master1
is和列的所有者表是:DetailTable1
MasterId
internal class Detail1Map : ClassMapping<Detail1Entity>
{
...
ManyToOne(x => x.Master1, map => { map.Column("MasterId"); });
推荐阅读
- javascript - 如何在 BootStrap 中创建一个与边缘有间距的进度条
- python - 如何在另一种方法中查看实例属性的更改版本?Python
- reactjs - Spring boot:提供多页面反应内容
- c++ - 在结构上应用标准算法?
- excel - 基于 Pivot 过滤器将数据保存在新工作簿中的 VBA 代码
- fortran - Fortran 中的指针数组
- node.js - TypeORM + Postgres:服务器重新启动后删除的可空列中的值?
- algorithm - Dart 二进制搜索功能不起作用
- pycharm - 我正在尝试使用 pycharm 创建一个新项目,但是
- discord - 如何禁止用户使用 discord.py?