活动记录 - HasAndBelongsToMany 不起作用
我在 SQL Server 数据库中有 3 个表。
我正在使用 Active Record for .NET,但遇到以下问题:当我创建包含附件的新用户时,UserAttachment 表中的数据未保存。它给了我以下异常:“无法将 NULL 值插入到表“UsersDB.dbo.UsersAttachments”的列“UserID”中;列不允许为空值。INSERT 失败。” 该声明已终止。
我假设 UserID 假设是自动填充的,因为该列是生成自动增量数字的主键。
在用户表中数据被保存,但不在用户附件中
请查看列名称后面的表名称。
用户 -用户身份 -姓名 -姓氏
用户附件 -用户附件ID -用户身份 -附件类型ID -姓名 -文件名路径
附件类型 -附件类型ID -TypeName
[PrimaryKey(PrimaryKeyType.Native, "UserID")]
public int UserID{ get; set; }
[Property(NotNull = false)]
public string Name { get; set; }
[Property(NotNull = false)]
public string LastName{ get; set; }
[HasAndBelongsToMany(
typeof(Attachments.Attachments),
Table = "UserAttachments",
ColumnKey = "UserAttachmentID",
ColumnRef = "UserID",
Cascade=ManyRelationCascadeEnum.All,
Inverse = true,
)
]
public IList<Users.Attachments> UserAttachments { get; set; }
你知道我做错了什么吗?
大卫
I have 3 tables in SQL Server Database.
I am using Active Record for .NET and I am having the problem that data in the UserAttachment table is not being saved when I create a new User including an attachment. It is giving me the following exception: "Cannot insert the value NULL into column 'UserID', table 'UsersDB.dbo.UsersAttachments'; column does not allow nulls. INSERT fails."
The statement has been terminated.
I am assuming that UserID suppose is being filled automatically since that column is a PrimaryKey with AutoIncrement number generated.
In the User table data is saved but not in the UserAttachments
Please see the Table Name following the column names.
User
-UserID
-Name
-LastName
UserAttachments
-UserAttachmentID
-UserID
-AttachmentTypeID
-Name
-FilenamePath
AttachmentType
-AttachmentTypeID
-TypeName
[PrimaryKey(PrimaryKeyType.Native, "UserID")]
public int UserID{ get; set; }
[Property(NotNull = false)]
public string Name { get; set; }
[Property(NotNull = false)]
public string LastName{ get; set; }
[HasAndBelongsToMany(
typeof(Attachments.Attachments),
Table = "UserAttachments",
ColumnKey = "UserAttachmentID",
ColumnRef = "UserID",
Cascade=ManyRelationCascadeEnum.All,
Inverse = true,
)
]
public IList<Users.Attachments> UserAttachments { get; set; }
Have you got an idea what I am doing wrong?
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的第一件事是 ColumnRef 和 ColumnKey 的方式是错误的。
ColumnKey 是存储要映射的对象的 ID 的列的名称。在本例中是一个用户。因此 ColumnKey 应设置为“UserId”
ColumnRef 是引用您要映射到的内容的 ID。因此 ColumnRef 应设置为“UserAttachmentId”
我可以发现的另一个问题是属性中指定的类型应该是集合中对象的类型。您的集合包含
Users.Attachments
,但您已指定typeof(Attachments.Attachments)
最后,您的
Inverse=true
表示您的附件“拥有”用户。我不知道你的应用程序,但这似乎违反直觉。至于您的问题是由上述哪个问题引起的,我不完全确定。
这里有一些进一步的文档
http://www.castleproject.org/activerecord/documentation/trunk /usersguide/relations/hasandbelongs.html
The first thing I can spot is you have ColumnRef and ColumnKey are the wrong way around.
ColumnKey is the name of the column where the ID of the object you are mapping from is stored. In this case a User. So ColumnKey should be set to "UserId"
ColumnRef is the ID which references the thing you are mapping to. So ColumnRef should be set to "UserAttachmentId"
Another problem I can spot is that the type specified in the attribute should be the type of the object in the collection. Your collection contains
Users.Attachments
, yet you have specifiedtypeof(Attachments.Attachments)
Finally your
Inverse=true
says that your attachments "own" the Users. I do not know your app, but this seems counterintuitive.As for which of the above issues is causing your problem, I am not entirely sure.
Some further documentation here
http://www.castleproject.org/activerecord/documentation/trunk/usersguide/relations/hasandbelongs.html