手动设置ID
我有一个用户表,其中“ID”字段是 GUID 字段。
我正在使用 ASPNET 创建一个用户帐户并获取 GUID。我正在尝试使用该 GUID 作为主 ID 在表中创建关联记录。
我遇到的问题是,当我手动设置 Users.ID 时,NHibernate 尝试执行更新,而不是插入。在 NHibernate Profiler 因“意外行数:0;预期:1”而崩溃之前,我看到了这一点。
我的 Users 表的 UsersMap 看起来像:
public class UsersMap : ClassMap<Users>
{
public UsersMap()
{
Id(x => x.ID, "ID"); //GUID
Map(x => x.Name, "Name"); //string
Map(x => x.PhoneNumber, "PhoneNumber"); //string
Map(x => x.FaxNumber, "FaxNumber"); //string
Map(x => x.EmailAddress, "EmailAddress"); //string
HasMany<UsersAddressBook>(x => x.usersAddressBook).KeyColumn("ID");
}
}
有什么想法吗?提前致谢。
I have a Users table where the "ID" field is a GUID field.
Using ASPNET I am creating a user account and getting the GUID back. I am trying to create associated records in my tables with that GUID as the main ID.
The problem I am running into is that when I manually set Users.ID NHibernate tries to do an Update, not an Insert. I see this with the NHibernate Profiler before it bombs out with "Unexpected row count: 0; Expected: 1".
My UsersMap for the table Users looks like:
public class UsersMap : ClassMap<Users>
{
public UsersMap()
{
Id(x => x.ID, "ID"); //GUID
Map(x => x.Name, "Name"); //string
Map(x => x.PhoneNumber, "PhoneNumber"); //string
Map(x => x.FaxNumber, "FaxNumber"); //string
Map(x => x.EmailAddress, "EmailAddress"); //string
HasMany<UsersAddressBook>(x => x.usersAddressBook).KeyColumn("ID");
}
}
Any ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要指定将分配您的 ID。
这将允许您指定该值,而 NHibernate 无需尝试执行更新。
You need to specify that your id will be assigned.
This will allow you to specify the value, without NHibernate trying to perform an update.
ISession.Save 有一个重载,允许您指定标识符。尝试使用它,不要手动设置你的 id 属性。
ISession.Save has an overload that allows you to specify identifier. Try using it, and don't set your id property manually.
作为 James 答案的附加值,我使用了:
请注意,default_value 是您的 Id 类型的默认值
在我这里是
0L
因为我使用long
作为我的 IDAs added value to James's answer I used:
Note that default_value is default value for your Id type
as in me is
0L
because I uselong
for my Id