Abdulmajeed Alshari

Abdulmajeed Alshari

  • 1.4k
  • 266
  • 56.9k

solve db.SaveChanges() exception MVC5

Jan 15 2018 3:33 PM
I am using code first approach when call SaveChanges function appears Exception message ,my Code is :
  1. public ActionResult Create(JobSeekerMore AllJobSeekerInfos, HttpPostedFileBase[] acadimicCertificate)  
  2.     {  
  3.         HttpPostedFileBase img;  
  4.         JobSeekerInfo jobSeekerInfo = new JobSeekerInfo();  
  5.         var userID = User.Identity.GetUserId();  
  6.         string path;  
  7.         string fileName;  
  8.         int i = 0;  
  9.         AcadimicDetail acadimi;  
  10.         if (ModelState.IsValid)  
  11.         {  
  12.             i = 0;  
  13.             foreach (AcadimicDetail item in AllJobSeekerInfos.MoreAcadimicDetials)  
  14.             {  
  15.                 acadimi = new AcadimicDetail();  
  16.                 acadimi = item;  
  17.                 fileName = userID + "acadimicCertificate";  
  18.                 acadimi.acadimicCertificate = fileName;  
  19.   
  20.                 img = acadimicCertificate[i];  
  21.                 path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);  
  22.                 img.SaveAs(path);  
  23.                 ++i;  
  24.                 acadimi.JobSeekerId = userID;  
  25.                 db.AcadimicDetails.Add(acadimi);  
  26.                 try  
  27.                 {  
  28.                     db.SaveChanges();  
  29.                 }  
  30.                 catch (Exception ee)  
  31.                 {  
  32.                     string mes = ee.InnerException.Message;  
  33.                     Exception d=new Exception();                         
  34.                     object f  = ee.InnerException.Data;  
  35.                     f = ee.InnerException.HelpLink;  
  36.                     f = ee.InnerException.HResult;  
  37.                     f = ee.InnerException.Source;  
  38.                     f = ee.HelpLink;  
  39.                     f = ee.HResult;  
  40.                     f = ee.Source;  
  41.                     f = ee.Data;  
  42.                     string tem = ee.StackTrace;  
  43.                 }  
  44.             }  

 and The Model of Table is as :
  1. public class AcadimicDetail  
  2. {  
  3.     public int Id { getset; }  
  4.     public string acadimicName { getset; }  
  5.     public string acadimicType { getset; }  
  6.     public string acadimicTitle { getset; }  
  7.     public DateTime acadimicEndDate { getset; }  
  8.     public string acadimicCountry { getset; }  
  9.     public string acadimicCity { getset; }  
  10.     public string acadimicCertificate { getset; }  
  11.     public string JobSeekerId { getset; }  

and the JobSeekerMore ViewModel is as :
  1. public class JobSeekerMore  
  2. {  
  3.     public List MoreAcadimicDetials { get { return _MoreAcadimicDetials; } }  
  4.     private List _MoreAcadimicDetials = new List();  

Now when Enter the below values into AcadimicDetail table are as :

Id=0 JobSeekerId="66a41cbf-0545-4a31-bc95-634af3a6b16b"
acadimicCertificate = "66a41cbf-0545-4a31-bc95-634af3a6b16bacadimicCertificate"
acadimicCity = "New Yourk"
acadimicCountry="USA"
acadimicEndDate = {1/1/0001 12:00:00 AM}
acadimicName="Teacher"
acadimicTitle="English"
acadimicType = "female"
the Exception Details are as :
InnerException Message "An error occurred while updating the entries. See the inner exception for details."
HResult = -2146233087
InnerException.Source = "EntityFramework"
StackTrace
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at jobPortal.Controllers.JobSeekerInfoesController.Create(JobSeekerMore AllJobSeekerInfos, HttpPostedFileBase[] acadimicCertificate) 

Answers (3)