流畅的 NHibernate - HasManyToMany 可能具有多个 ChildKeyColumns 吗?
我有两种实体:评论和任务。用户可以为这两种类型添加书签。所以我创建了一个名为“Bookmarks”的表:
CREATE TABLE [dbo].[Bookmarks] (
[ID] [int] IDENTITY(1,1) NOT NULL,
[FKUserID] [int] NOT NULL,
[FKCommentID] [int] NULL,
[FKTaskID] [int] NULL)
我的 User 实体的这些关系的映射如下所示:
var bookmarks = Component(x => x.Bookmarks, x => x.HasManyToMany(b => b.Comments)
.Table("Bookmarks")
.Access.CamelCaseField()
.ParentKeyColumn("FKUserID")
.ChildKeyColumn("FKCommentID")
.AsBag()
.LazyLoad());
bookmarks.HasManyToMany(b => b.Tasks)
.Table("Bookmarks")
.Access.CamelCaseField()
.ParentKeyColumn("FKUserID")
.ChildKeyColumn("FKTaskID")
.AsBag()
.LazyLoad();
我认为一切都会很好地工作,但当数据库对象是从 NHibernate 本身从头创建时就不行了。然后我收到了很好的错误消息
NHibernate.Exceptions.GenericADOException:无法插入集合:[Domain.Model.User.Domain.Model.User.Bookmarks.Tasks#1][SQL:插入书签(FKUserID,FKTaskID)值(@p0,@ p1)] ----> System.Data.SqlClient.SqlException:无法将值 NULL 插入表“xyzautomatedtests.dbo.Bookmarks”的“FKCommentID”列;列不允许为空。插入失败。 该声明已终止。
当然,我可以使用两个表而不是这个表,但是有没有办法让它运行?不幸的是,我找不到将配置中的这两个子列设置为可为空的方法。
有什么提示吗?
I have got two kind of entities, comments and tasks. Both types can be bookmarked by a user. So I have created a table called 'Bookmarks':
CREATE TABLE [dbo].[Bookmarks] (
[ID] [int] IDENTITY(1,1) NOT NULL,
[FKUserID] [int] NOT NULL,
[FKCommentID] [int] NULL,
[FKTaskID] [int] NULL)
The mapping of these relations for my User entity looks like this:
var bookmarks = Component(x => x.Bookmarks, x => x.HasManyToMany(b => b.Comments)
.Table("Bookmarks")
.Access.CamelCaseField()
.ParentKeyColumn("FKUserID")
.ChildKeyColumn("FKCommentID")
.AsBag()
.LazyLoad());
bookmarks.HasManyToMany(b => b.Tasks)
.Table("Bookmarks")
.Access.CamelCaseField()
.ParentKeyColumn("FKUserID")
.ChildKeyColumn("FKTaskID")
.AsBag()
.LazyLoad();
That all would work nicely I think, but not when the database objects are created by scratch from NHibernate itself. Then I get the nice error message
NHibernate.Exceptions.GenericADOException : could not insert collection: [Domain.Model.User.Domain.Model.User.Bookmarks.Tasks#1][SQL: INSERT INTO Bookmarks (FKUserID, FKTaskID) VALUES (@p0, @p1)]
----> System.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'FKCommentID', table 'xyzautomatedtests.dbo.Bookmarks'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Sure I could have been using two tables instead of this one, but isn't there a way to get this running? Unfurtunately I can't find a way to set these two child columns in the configuration to be nullable.
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仔细检查您的表定义。 SQLException 直接来自数据库,因此您遇到的问题来自数据库,而不是来自 .Net。
只需确保书签表中的 FKCommentID 列实际上可以为 Null。
Double check your table definition. The SQLException comes straight from the database, so the problems that you're having are from the database, not from .Net.
Just make sure that the FKCommentID column in your Bookmarks table is actually Nullable.