使用空 guid PetaPoco 保存新对象无法执行插入

发布于 2024-12-27 08:27:44 字数 518 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酉 2025-01-03 08:27:44

对于原始海报来说,回答这个问题很可能已经太晚了,但对于其他人来说......
我的理解是“保存”不适用于 Guid 列。 petaPoco 判断是插入还是更新的方法是看键值是否与默认值不同。如果是数字,则为 0;如果是 Guid,则为 '00000000-0000-0000-0000-000000000000'。
以下是 IsNew 方法的摘录

// Common primary key types
            if (type == typeof(long))
                return (long)pk == 0;
            else if (type == typeof(ulong))
                return (ulong)pk == 0;
            else if (type == typeof(int))
                return (int)pk == 0;
            else if (type == typeof(uint))
                return (uint)pk == 0;
// Create a default instance and compare
return pk == Activator.CreateInstance(pk.GetType());

对于 Guid,最后一个语句将始终返回 false,即使您的主键全为零。如果这样的话,确实应该有另一个其他的:

else if (type == typeof(Guid))
                return Guid.Equals(pk, Activator.CreateInstance(pk.GetType()));

但是,我没有检查这是否是唯一需要更改才能使 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

// Common primary key types
            if (type == typeof(long))
                return (long)pk == 0;
            else if (type == typeof(ulong))
                return (ulong)pk == 0;
            else if (type == typeof(int))
                return (int)pk == 0;
            else if (type == typeof(uint))
                return (uint)pk == 0;
// Create a default instance and compare
return pk == Activator.CreateInstance(pk.GetType());

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:

else if (type == typeof(Guid))
                return Guid.Equals(pk, Activator.CreateInstance(pk.GetType()));

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.

夜血缘 2025-01-03 08:27:44

我能够通过从 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.

極樂鬼 2025-01-03 08:27:44

我从未使用过 petapoco,但我首先尝试的一些事情是: -

  1. Primkey 列在 sql 中的数据类型是“Uniqueidentifier”
  2. Primkey 列的默认值是 (newid())
  3. 将 PrimKey 更改为 Get/Set 值并执行 '保存前 Primkey = Guid.NewGuid'。

编辑:我只是想,当你声明一个新的保留时,它是否允许你在构造函数上传递一个 ID 等?

I've never worked with petapoco but a few things I would try first are:-

  1. Primkey column's Data Type in sql is 'Uniqueidentifier'
  2. Primkey column's Default Value is (newid())
  3. Change the PrimKey to a Get/Set value and do 'Primkey = Guid.NewGuid' before saving.

EDIT: I just thought, when you declare a New Reservation does it allow you to pass an ID ect on the constructor?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文