使用空 guid PetaPoco 保存新对象无法执行插入
我正在尝试使用 petapoco 将新对象保存到数据库中。 我有一个使用 Guid 作为主键的对象。
这是对象主键属性的示例。
<PetaPoco.Column()> _
Public ReadOnly Property Primkey() As Guid Implements IStandardDocument.Primkey
Get
Return (_primkey)
End Get
End Property
这是保存方法的示例。
database.Save("Reservations", "Primkey", reservation)
生成的 sql 是主键为 00000000-0000-0000-0000-000000000000 的更新,而不是插入。所以永远不会插入新记录。
如果我有一个具有有效指南的现有对象,则更新和删除所有工作都可以正常进行。
我在保存案例中做错了什么?
I am trying to save a new object to the database using petapoco.
I have an object that uses a Guid for the primary key.
Here is an example of the objects primary key property.
<PetaPoco.Column()> _
Public ReadOnly Property Primkey() As Guid Implements IStandardDocument.Primkey
Get
Return (_primkey)
End Get
End Property
Here is an example of the save method.
database.Save("Reservations", "Primkey", reservation)
The sql that is generated is an update with a primkey of 00000000-0000-0000-0000-000000000000 instead of an insert. So new records are never inserted.
If I have an existing object with a valid guid the update and delete all work correctly.
What am I doing wrong for the save case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于原始海报来说,回答这个问题很可能已经太晚了,但对于其他人来说......
我的理解是“保存”不适用于 Guid 列。 petaPoco 判断是插入还是更新的方法是看键值是否与默认值不同。如果是数字,则为 0;如果是 Guid,则为 '00000000-0000-0000-0000-000000000000'。
以下是 IsNew 方法的摘录
对于 Guid,最后一个语句将始终返回 false,即使您的主键全为零。如果这样的话,确实应该有另一个其他的:
但是,我没有检查这是否是唯一需要更改才能使 GUID 密钥起作用的事情。插入记录后,PetaPoco 尝试检查新插入记录的键值,也可能会失败。
Most likely it's too late to answer this for the original poster, but for others out there...
My understanding is the Save doesn't work for Guid columns. The way petaPoco knows if it's an insert vs update is if the key value is different from default. In case if numeric it would be 0, in the case of Guid it would be '00000000-0000-0000-0000-000000000000'.
Here's an extract of IsNew method
In the case of Guid the last statement would always return false, even if your primary key is all zeros. There should really be another else if as such:
However, I didn't check if this is the only thing that has to be changed for the GUID key to work. After inserting the record, PetaPoco tries to check the key value of the newly inserted record and it might fail on that too.
我能够通过从 primkey 属性中删除类型来完成这项工作。
将 primkey 设置为 null 现在将返回 null 而不是空 guid。
PetaPoco 现在看到主键字段为空并生成正确的插入语句,而不是为 primkey 00000000-0000-0000-0000-000000000000 生成更新语句。
I was able to make this work by removing the type from the primkey property.
Setting the primkey to null will now return null rather than an empty guid.
PetaPoco now sees the primarykey field is a null and generate the correct insert statement rather than generating an update statement for primkey 00000000-0000-0000-0000-000000000000.
我从未使用过 petapoco,但我首先尝试的一些事情是: -
编辑:我只是想,当你声明一个新的保留时,它是否允许你在构造函数上传递一个 ID 等?
I've never worked with petapoco but a few things I would try first are:-
EDIT: I just thought, when you declare a New Reservation does it allow you to pass an ID ect on the constructor?