NPOCO : NullReferenceException을 던지고, 내 삭제 () 호출 이유는 무엇입니까?

StackOverflow https://stackoverflow.com//questions/25082377

  •  02-01-2020
  •  | 
  •  

문제

NPOCO의 delete () 메서드를 사용하여 데이터베이스에서 행을 삭제하려고합니다.그러나 그것은 단지 NullReferenceException을 던집니다.

나는 해결 방법을 발견했지만 누군가가 ID로 삭제할 수있는 삭제 기능이 이유를 삭제할 수없는 이유는 무엇인지 알고 있는지 궁금합니다.이것은 여러 테이블에서 발생합니다.모든 테이블은 [PrimaryKey("ID")] Decorator를 사용하여 모델에 플래그가 지정된 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 저장소 의 소스를 따라 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 메소드로서의 첫 번째 2 줄 만 포함했습니다. ForObject(object o, string primaryKeyName) 는 다음과 같습니다. ID가 12345이면 테이블이 DuestItem으로 매핑되고 기본 키가 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에서 제공되는 다른 코드 경로를 따르는 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top