c# - nHibernate HasMany 与连接表
问题描述
我正在尝试使用 Join 表创建一对多关系,但我很难做到这一点。下面是 C# 中的表结构、实体和映射
CREATE TABLE tbl_configurations
(
strCode uniqueidentifier default NEWID(),
strName nvarchar(MAX) NOT NULL,
.....
CONSTRAINT pk_configurations PRIMARY KEY (strCode)
)
CREATE TABLE tbl_environments
(
strCode uniqueidentifier default NEWID(),
strName nvarchar(15) NOT NULL,
strDescription nvarchar(MAX) NOT NULL
CONSTRAINT pk_environments PRIMARY KEY (strCode)
)
CREATE TABLE tbl_configurationsenvironments
(
strConfiguration uniqueidentifier NOT NULL,
strEnvironment uniqueidentifier NOT NULL
CONSTRAINT pk_configurationsenvironments PRIMARY KEY (strConfiguration, strEnvironment),
CONSTRAINT fk_configurationsenvironmentsconfigurations FOREIGN KEY (strConfiguration) REFERENCES tbl_configurations(strCode),
CONSTRAINT fk_configurationsenvironmentsenvironments FOREIGN KEY (strEnvironment) REFERENCES tbl_environments(strCode)
)
public class ConfigurationEntity
{
public virtual Guid strCode { get; set; }
public virtual string strName { get; set; }
.....
public virtual ISet<EnvironmentEntity> colEnvironments { get; set; }
}
public class EnvironmentEntity
{
public virtual Guid strCode { get; set; }
public virtual string strName { get; set; }
public virtual string strDescription { get; set; }
}
public class EnvironmentMap : ClassMap<EnvironmentEntity>
{
public EnvironmentMap()
{
Table("tbl_environments");
Id(x => x.strCode).GeneratedBy.Guid();
Map(x => x.strName);
Map(x => x.strDescription);
Not.LazyLoad();
}
}
public class ConfigurationMap : ClassMap<ConfigurationEntity>
{
public ConfigurationMap()
{
Table("tbl_configurations");
Id(x => x.strCode).GeneratedBy.Guid();
Map(x => x.strName);
Join("tbl_configurationsenvironments", m =>
{
m.Fetch.Join();
m.KeyColumn("strConfiguration");
m.HasMany<EnvironmentEntity>(x => x.colEnvironments)
.AsSet()
.Cascade.All()
.KeyColumn("strCode")
.Fetch.Join()
.Not.LazyLoad();
});
Not.LazyLoad();
}
}
根据我在控制台上看到的,它正确映射了 tbl_configurations 和 tbl_configurationsenvironments 之间的关系
tbl_configurations confi11_ left outer join tbl_configurationsenvironments confi11_1_ on confi11_.strCode=confi11_1_.strConfiguration
但是它没有正确设置 tbl_environments 和 tbl_configurationenvironments 之间的关系
左外连接 tbl_environments colenviron12_ on confi11_.strCode=colenviron12_.strCode
相反,它在 tbl_configurations 和 tbl_environments 之间建立了关系,我很确定错误出现在 ConfigurationMap 上,尤其是在本节中:
m.HasMany<EnvironmentEntity>(x => x.colEnvironments)
.AsSet()
.Cascade.All()
.KeyColumn("strCode")
.Fetch.Join()
.Not.LazyLoad();
});
你知道我怎么能指定tbl_configurationsenvironments.strEnvironments之间的关系吗?我尝试使用 .PropertyRef("strEnvironment"),但是当我这样做时,它看起来 tbl_configurations 上的属性不正确
解决方案
推荐阅读
- install4j - install4j InvalidByteCodeException
- mysql - 在 SQL 中的链接服务器上使用 OPENQUERY 进行“INSERT IGNORE INTO”时出错
- php - PHP foreach 无法回显值
- c++ - Arduino struct 变量似乎在没有明确重新分配的情况下被重新分配
- javascript - 如何预填充下拉字段
- javascript - 尝试将文本框值添加为表格行
- recursion - 是否可以限制 S3 存储桶中递归目录列表的深度?
- css - chrome 中的 flex-basis 自动
- django - NoModuleName 'channels',但我已经安装了它。姜戈
- node.js - 为什么 Express 应用使用 server.js 和 app.js