流畅的nhibernate:保存带有子实体的新实体时出现插入错误
我正在尝试保存一个新实体“帖子”,并将 1 个项目添加到其“修订”列表中。
一个Post
可以有多个PostRevisions
,而一个PostRevision
只能有一个Post
。
我尝试了几种映射 PostRevisions 的方法,我的 PostRevisionMap 如下:
public PostRevisionMap()
{
Id(x => x.PostRevisionId, "PostRevisionId");
Map(x => x.Created, "CreateDateTime").Not.Nullable();
/// SOME OTHER STUFF HERE
References(x => x.Post, "PostId"); // OPTION 1
References(x => x.Post,"PostId").ForeignKey("PostId").PropertyRef(d => d.PostId); // OPTION 2
HasOne<Post>(x => x.Post).ForeignKey("PostId").Cascade.All().PropertyRef(x => x.PostId); // OPTION 3
}
调用 SaveOrUpate 时,我收到不同的错误
OPTION 1 & 3 原因
The INSERT statement conflicted with the FOREIGN KEY constraint "PostId".
选项 2 原因
NHibernate.HibernateException : Unable to resolve property: PostId
我的 PostMap 如下:
public PostMap()
{
Id(x => x.PostId).Column("PostId");
HasMany(x => x.Revisions)
.Cascade.All()
.Table("PostRevisions")
.KeyColumn("PostId")
.ForeignKeyConstraintName("FK_PostRevision_Post");
/// OTHER STUFF
}
任何人都可以指出我正确的方向,因为我无法发现问题。 TIA
I am trying to save a new entity 'Post' with 1 item added to its 'Revisions' List.
A Post
can have many PostRevisions
, and a PostRevision
can only have one Post
.
I have tried several ways of mapping the PostRevisions, my PostRevisionMap is as follows:
public PostRevisionMap()
{
Id(x => x.PostRevisionId, "PostRevisionId");
Map(x => x.Created, "CreateDateTime").Not.Nullable();
/// SOME OTHER STUFF HERE
References(x => x.Post, "PostId"); // OPTION 1
References(x => x.Post,"PostId").ForeignKey("PostId").PropertyRef(d => d.PostId); // OPTION 2
HasOne<Post>(x => x.Post).ForeignKey("PostId").Cascade.All().PropertyRef(x => x.PostId); // OPTION 3
}
When calling SaveOrUpate I get a different errors
OPTION 1 & 3 cause
The INSERT statement conflicted with the FOREIGN KEY constraint "PostId".
OPTION 2 causes
NHibernate.HibernateException : Unable to resolve property: PostId
My PostMap is as follows:
public PostMap()
{
Id(x => x.PostId).Column("PostId");
HasMany(x => x.Revisions)
.Cascade.All()
.Table("PostRevisions")
.KeyColumn("PostId")
.ForeignKeyConstraintName("FK_PostRevision_Post");
/// OTHER STUFF
}
Can anyone point me in the right direction as I can not spot the issue.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
PostMap
PostRevisionMap:
Try this:
PostMap
PostRevisionMap: