1

DeleteOnSubmit() メソッドを使用して行を削除する必要がある WP7 アプリに取り組んでいますが、NullReferenceException エラーが発生し続けます。問題がどこにあるのかわかりません。

public void HardDeleteOrder(int deleteOrderId)
{

    var oResult = from o in App.orderDataContext.orders
              where o.OrderId == deleteOrderId
              select o;


    foreach (var oRow in oResult)
    {
       App.orderDataContext.Orders.DeleteOnSubmit(oRow);
    }
    App.orderDataContext.SubmitChanges();
}

これを実行すると、コードはメソッドの終了ブレースでクラッシュし、「NullReferenceException was unhandled」という例外メッセージが表示されます。

スタック トレースは次のとおりです。

System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
   at System.Data.Linq.Mapping.MetaAccessor`2.SetBoxedValue(Object& instance, Object value)
   at System.Data.Linq.ChangeProcessor.ClearForeignKeysHelper(MetaAssociation assoc, Object trackedInstance)
   at System.Data.Linq.ChangeProcessor.ClearForeignKeyReferences(TrackedObject to)
   at System.Data.Linq.ChangeProcessor.PostProcessUpdates(List`1 insertedItems, List`1 deletedItems)
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges()
   at orders.viewmodels.OrderViewModel.HardDeleteOrder(Int32 deleteOrderId)
   at orders.OrderView.RemoveOrderFromDatabase()
   at orders.OrderView.RemoveOrder()
   at orders.OrderView.detailsBarCancel_Click(Object sender, EventArgs e)
   at orders.App.detailsBarCancel_Click(Object sender, EventArgs e)
   at Microsoft.Phone.Shell.ApplicationBarItemContainer.FireEventHandler(EventHandler handler, Object sender, EventArgs args)
   at Microsoft.Phone.Shell.ApplicationBarIconButton.ClickEvent()
   at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.ClickEvent()
   at Microsoft.Phone.Shell.ApplicationBar.OnCommand(UInt32 idCommand)
   at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 idCommand)

ここで何が欠けていますか?

4

1 に答える 1

0

これは主に外部キー制約によるものです。http://msdn.microsoft.com/en-us/library/bb386925.aspxによると

LINQ to SQL は、カスケード削除操作をサポートまたは認識しません。制約のあるテーブルの行を削除する場合は、次のいずれかのタスクを完了する必要があります。

Set the ON DELETE CASCADE rule in the foreign-key constraint in the database.

Use your own code to first delete the child objects that prevent the parent object from being deleted.
于 2012-09-23T12:05:31.240 に答える