gpt4 book ai didi

c# - NPoco:为什么我的 Delete() 调用会抛出 NullReferenceException?

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:43 25 4
gpt4 key购买 nike

我正在尝试使用 NPoco 的 Delete() 方法从数据库中删除一行。然而它只是抛出一个 NullReferenceException。

我找到了解决方法,但我想知道是否有人知道为什么内置的按 ID 删除的删除功能似乎对我不起作用。这发生在多个表上。我的所有表都有一个称为 ID 的标准整数主键,它已在模型中使用 [PrimaryKey("ID")] 装饰器标记。

对象引用未设置到对象的实例。

Delete<PurchaseItem>(id); // throws null reference exception.
Delete<PurchaseItem>("where id = @0", id); // works.

传递的 id 有效且项目在数据库中。代码没有执行任何 SQL。

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
NPoco.PocoDataFactory.ForObject(Object o, String primaryKeyName) in d:\Adam\projects\NPoco\src\NPoco\PocoDataFactory.cs:41
NPoco.Database.Delete(String tableName, String primaryKeyName, Object poco, Object primaryKeyValue) in d:\Adam\projects\NPoco\src\NPoco\Database.cs:1587
NPoco.Database.Delete(Object pocoOrPrimaryKey) in d:\Adam\projects\NPoco\src\NPoco\Database.cs:1598
Harmsworth.DAL.HarmsworthDB.DeletePurchaseItemFromBasketByID(Int32 id) in c:\inetpub\wwwroot\harmsworth\Website\classes\HarmsworthDAL.cs:224
Harmsworth.ViewBasketPage.RemoveItem(Int32 id) in c:\inetpub\wwwroot\harmsworth\Website\view-basket.aspx.cs:187
Harmsworth.ViewBasketPage.PurchaseItemsRepeater_ItemCommand(Object sender, RepeaterCommandEventArgs e) in c:\inetpub\wwwroot\harmsworth\Website\view-basket.aspx.cs:75
System.Web.UI.WebControls.Repeater.OnItemCommand(RepeaterCommandEventArgs e) +111
[more redundant trace info]

最佳答案

按照 GitHub repository 中的来源看起来 NPoco 中有一个错误:

你还没有指定什么类型id是,但我假设它是一个 int 并且您有以下代码:

var id = 12345;
Delete<PurchaseItem>(id);

调用 NPoco Delete<T>(object pocoOrPrimaryKey) ,其代码为:

public int Delete<T>(object pocoOrPrimaryKey)
{
if (pocoOrPrimaryKey.GetType() == typeof(T))
return Delete(pocoOrPrimaryKey);
var pd = PocoDataFactory.ForType(typeof(T));
return Delete(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, null, pocoOrPrimaryKey); // This is the method your code calls
}

依次调用 NPoco Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue) ,其代码为:

public virtual int Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
{
if (!OnDeleting(new DeleteContext(poco, tableName, primaryKeyName, primaryKeyValue))) return 0;
var pd = PocoDataFactory.ForObject(poco, primaryKeyName);
...
}

我只包含了前两行,因为它是 PocoDataFactory.ForObject根据您的堆栈跟踪抛出异常的方法。 ForObject(object o, string primaryKeyName) 的代码是:

public PocoData ForObject(object o, string primaryKeyName)
{
var t = o.GetType(); // This is where the exception comes from
...
}

这是正在发生的事情(假设 id 是 12345,表被映射为 PurchaseItem 并且主键被映射为 Id):

Delete<PurchaseItem>(pocoOrPrimaryKey : 12345);
Delete(tableName: "PurchaseItem", primaryKeyName: "Id", poco: null, primaryKeyValue: 12345);
PocoDataFactory.ForObject(o: null, primaryKeyName: Id);

Delete<PurchaseItem>("where id = @0", id);的原因有效,是它遵循不同的代码路径,其中用于解析 PocoData 的类型来自typeof(T)其中 TPurchaseItem .

关于c# - NPoco:为什么我的 Delete<Model>() 调用会抛出 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com