2017-09-03 50 views
0

我是新來的實體框架和LINQ。我被困在一個問題,我需要首先檢查記錄是否已經存在,如果存在,那麼我需要相應地更新列RESUMEID的記錄。如果沒有,那麼我需要添加記錄。我能夠成功添加,但我不知道如何更新LINQ中的記錄。如何在我的ASP.NET MVC 5項目中使用LINQ LAMBDA更新記錄

下面是我的嘗試:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(ReferralViewModel viewModel) 
    { 
     var candidateId = User.Identity.GetUserId(); 
     // I AM CONFUSED ABOUT BELOW STATEMENT 
     var IsDupeReferral = _context.Referrals 
     .Where(r => (r.CandidateId == candidateId) 
     && (r.CompanyId == viewModel.CompanyId) && (r.SkillId == viewModel.SkillId)) 
     .Select(r=>r.ReferralId).SingleOrDefault(); 

     if(IsDupeReferral!=0) 
     { 
      //IF I FIND DUPE REFERRAL RECORD I WANT TO UPDATE SOME OF THE VALUES IN THAT 
        _context.Referrals.Where(r => r.ReferralId == IsDupeReferral). 
      AND UPDATE r.resumeId with viewModel.ResumeId // How to do this? 
      // NOT SURE ABOUT BELOW LINE EITHER 
      _context.SaveChanges(); 
     } 
     else 
     { 
      // BELOW CODE IS WORKING FINE 
      var referral = new Referral 
      { 
       ReferralName = viewModel.ReferralName, 
       ResumeId = viewModel.ResumeId, 
       CandidateId = candidateId, 
       DegreeId = viewModel.DegreeId, 
       CoverLetterId = viewModel.CoverLetterId, 
       SkillId = viewModel.SkillId 
      }; 

      if (!string.IsNullOrEmpty(viewModel.TempCompany)) 
      { 
       var newCompany = new Company 
       { 
        CompanyName = viewModel.TempCompany 
       }; 
       newCompany.Referrals.Add(referral); 
       _context.Companies.Add(newCompany); ; 
      } 
      else 
      { 
       referral.CompanyId = viewModel.CompanyId.Value; 
       _context.Referrals.Add(referral); 
      } 
      _context.SaveChanges(); 
     } 

     return RedirectToAction("ReferralCenter"); 
    } 

回答

1

這裏的解決方案

//IF I FIND DUPE REFERRAL RECORD I WANT TO UPDATE SOME OF THE VALUES IN THAT 
var referral = _context.Referrals.FirstOrDefault(r => r.ReferralId == IsDupeReferral); 
// AND UPDATE r.resumeId with viewModel.ResumeId 
if (referral !=null) { 
    referral.resumeId = viewModel.ResumeId; 
    _context.Entry(referral).State = System.Data.EntityState.Modified; 
    _context.SaveChanges(); 
} 

其實,你並不需要得到IsDupeReferral,然後再次請求記錄。嘗試將您的代碼組合如下:

var referral = _context.Referrals 
     .Where(r => (r.CandidateId == candidateId) 
     && (r.CompanyId == viewModel.CompanyId) && (r.SkillId == viewModel.SkillId)).SingleOrDefault(); 

    if (referral !=null) { 
     referral.resumeId = viewModel.ResumeId; 
     _context.Entry(referral).State = System.Data.EntityState.Modified; 
     _context.SaveChanges(); 
    } 
    else { 

    // add a new record 
    } 
+1

先生,這是必要的嗎? '_context.Entry(referral).State = System.Data.EntityState.Modified;' – Unbreakable

+0

以下答案用途。 FirstOrDefault。你能否告訴我應該使用哪一個。 FirstOrDefault還是SingleOrDefault? – Unbreakable

+1

EF是不是足夠聰明,知道我正在修改一行?或者我們需要明確設置行狀態? – Unbreakable

1
Referral referral = _context.Referrals.FirstOrDefault(r=> r.ReferralId = SomeId); 
if(referral == null) // then referral does not exist - add it 
{ 
    referral = new Referral{ 
       ReferralName = viewModel.ReferralName, 
       ResumeId = viewModel.ResumeId, 
       CandidateId = candidateId, 
       DegreeId = viewModel.DegreeId, 
       CoverLetterId = viewModel.CoverLetterId, 
       SkillId = viewModel.SkillId 
      }; 
    _context.Referrals.Add(referral); 
} 
else // referral already exists - update its values 
{ 
    //make changes to referral 
    referral.ReferralName = viewModel.ReferralName; 
    referral.ResumeId = viewModel.ResumeId; 
    referral.CandidateId = candidateId; 
    referral.DegreeId = viewModel.DegreeId; 
    referral.CoverLetterId = viewModel.CoverLetterId; 
    referral.SkillId = viewModel.SkillId; 

} 
_context.SaveChanges(); //no matter added or updated - save the changes 
+0

先生,我希望如果多個記錄匹配時拋出異常。我的意思是如果我的應用程序必須正確工作,那麼ATMOST 1的推薦結果是可能的。那麼ShallI使用FirstorDefault還是SingleOrDerfault? – Unbreakable

+0

結果將是一個或零。我應該使用「SingleorDefault」還是「FirstOrDefault」 – Unbreakable

+0

我剛剛在wiki中閱讀過,它說如果我從「FirstOrDefault」獲取多條記錄,將不會拋出異常。你能指導我 – Unbreakable

相關問題