2016-05-21 94 views
0

我有有許多字段的表DivorceCases,它的模式是這樣的:實體框架6更新,其中外鍵引用NULL

[Key] 
    [Required] 
    public string case_id { get; set; } 
    public string user_id { get; set; } 
    public DateTime case_date { get; set; } 
    public virtual Transactions t { get; set; } 

我試圖交易細節添加到現有行,但我得到一個[NullReferenceException:對象引用未設置爲對象的實例。]錯誤。在DivorceCases表中,EF6爲Transactions表的外鍵引用創建了一個列t_id。

Snapshot of DivorceCases Table

我用:

public static void UpdatePayment(string cid,string txnno) 
    { 
DivorceCasesContext db = new DivorceCasesContext(); 
      DivorceCases dc = db.DivorceCase.Where(x => x.case_id == cid).Include(x => x.t).SingleOrDefault(); 
      if (dc.t == null || dc.t.txn_id1 == null || dc.t.txn_id1 == "") 
      { 
       dc.t.txn_id1 = txnno; 
       dc.t.amount1 = amount.ToString(); 
       dc.t.date1 = DateTime.Now.ToString(); 
      } 
      else 
      { 
       dc.t.txn_id2 = txnno; 
       dc.t.amount2 = amount.ToString(); 
       dc.t.date2 = DateTime.Now.ToString(); 
      } 
      db.Set<DivorceCases>().Attach(dc); 
      db.Entry(dc).State = EntityState.Modified; 
      db.SaveChanges(); 
     } 
} 

我得到的錯誤是:

[NullReferenceException: Object reference not set to an instance of an object.] 
    gonylaw.Helpers.Amounts.UpdatePayment(String cid, String txnno) +425 
    gonylaw.Controllers.PaymentController.PaymentProcessor(CreditCard c) +461 
    lambda_method(Closure , ControllerBase , Object[]) +104 
    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 
    System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 
    System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50 
    System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225 
    System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 
    System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26 
    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 
    System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9644037 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 
+0

是這應該是一個交易的集合?也就是說,'DivorceCase'和'Transaction'之間的關係是一對多的關係嗎? –

+0

@MarcL。不是一對一的 –

+0

哪一行是錯誤? –

回答

2

您正在訪問的dc.t性能沒有保證dc.t不爲空。如果第一個條件if聲明短路,dc.t是空的,但你嘗試給一個值dc.t.txn_id

if(dc.t == null || ...) 
{ 
    dc.t.txn_id1 = txnno; // null ref exception here 
} 

從Asside,還有其他方面的改進,我建議:

public static void UpdatePayment(string cid,string txnno) 
{ 
    // add using block to dispose of DbContext after you're done with it 
    using(DivorceCasesContext db = new DivorceCasesContext()) 
    { 
     DivorceCases dc = db.DivorceCase.Include(x => x.t) 
      .Where(x => x.case_id == cid) 
      .SingleOrDefault(); 

      // take care of your null ref problem 
      if(dc.t == null) 
      { 
       dc.t = new Transactions(); 
      } 

      // simplify null/empty string check 
      if(string.IsNullOrEmpty(dc.t.txn_id1)) 
      { 
       dc.t.txn_id1 = txnno; 
       dc.t.amount1 = amount.ToString(); 
       dc.t.date1 = DateTime.Now.ToString(); 
      } 
      else 
      { 
       dc.t.txn_id2 = txnno; 
       dc.t.amount2 = amount.ToString(); 
       dc.t.date2 = DateTime.Now.ToString(); 
      } 

      // this is not needed - dc is already attached to the context 
      // (unless you're doing something nonstandard by default 
      // within your DbContext) 
      //db.Set<DivorceCases>().Attach(dc); 
      //db.Entry(dc).State = EntityState.Modified; 

      db.SaveChanges(); 
     } 
    } 
} 
+0

天才...你救了我...非常感謝..它工作:-) –

+0

與您的代碼唯一的問題是你不能使用這個離婚案件dc = db.DivorceCase.Include(x => xt).Find( cid);這是無效的。相反離婚案件dc = db.DivorceCase.Find(cid);像魅力一樣... –

+0

你是對的;在切換到「Find」調用之前,我改變了'Include'語句的位置 - 這使得'.Where(...)。SingleOrDefault()'正確用於您的目的。我會糾正答案。 – Moho