我正在尝试使用复合键插入记录(不是我的数据库设计,我不得不使用企业架构,所以在这里更改数据库不是一个选项。)
我有以下POCO课程
public class RulesEngineHeader
{
public virtual string CompanyCode { get; set; }
public virtual string RuleID { get; set; }
public virtual string RuleGroup { get; set; }
public virtual string RuleDescription { get; set; }
public virtual string Expression { get; set; }
}
和以下映射(使用Fluent)
internal class RulesEngineHeaderMap : ClassMap<RulesEngineHeader>
{
internal RulesEngineHeaderMap()
{
Table("LIOEP023");
Id(x => x.CompanyCode, "CONO23")
.GeneratedBy.Assigned()
.Length(2)
.Not.Nullable();
Id(x => x.RuleID, "RLID23")
.GeneratedBy.Assigned()
.Length(30)
.Not.Nullable();
Map(x => x.RuleGroup, "RGRP23")
.Length(30)
.Not.Nullable();
Map(x => x.RuleDescription, "RLDS23")
.Length(50)
.Not.Nullable();
Map(x => x.Expression, "EXPR23")
.Length(2500)
.Not.Nullable();
}
}
使用以下代码:
using (iSeriesUOW uow = new iSeriesUOW())
{
GenericRepository<RulesEngineHeader> rep = new GenericRepository<RulesEngineHeader>(uow);
RulesEngineHeader header = new RulesEngineHeader();
header.CompanyCode = "LI";
header.RuleID = "TEST";
header.RuleGroup = "AC";
header.RuleDescription = "Description";
header.Expression = "This is my rule expression";
rep.Add(header);
uow.Commit();
}
rep.Add只是做了一个
_uow.Session.Save(entityObject);
和uow.Commit();做了 _session.Flush();
if (_tx != null)
_tx.Commit();
执行此操作时,我会尝试执行以下查询:
INSERT INTO LIOEP023
(RGRP23,
RLDS23,
EXPR23,
RLID23)
VALUES ('AC' /* @p0_0 */,
'Description' /* @p1_0 */,
'This is my rule expression' /* @p2_0 */,
'TEST' /* @p3_0 */)
如您所见,我的CompanyCode / CONO23无处可寻,导致错误。
为什么在我的插入中没有使用这个想法?
答案 0 :(得分:1)
我发现我应该使用CompositeId而不是使用2 Id。
所以我的代码应该是这样的:
internal RulesEngineHeaderMap()
{
Table("LIOEP023");
CompositeId()
.KeyProperty(x => x.CompanyCode, "CONO23")
.KeyProperty(x => x.RuleID, "RLID23");
Map(x => x.RuleGroup, "RGRP23")
.Length(30)
.Not.Nullable();
Map(x => x.RuleDescription, "RLDS23")
.Length(50)
.Not.Nullable();
Map(x => x.Expression, "EXPR23")
.Length(2500)
.Not.Nullable();
}