Guest User

Guest User

  • Tech Writer
  • 611
  • 129.6k

Save record in database using linq.

Jul 28 2020 11:30 AM
{
"userInjuries": {
"userId": 4,
"userInjuriesList": [
{
"userInjuriesTypeId": 4,
"description": "ABC",
"date": "22/7/1990"
}
]
}
}
 
How to store this type Format Data in database.I have make a api but i have a problem.Please check me code.
it's me save code.
  1. [Produces("application/json")]  
  2. [Route("SavePhysicalInjueris")]  
  3. [AllowAnonymous]  
  4. public InjuerisResult SavePhysicalInjueris(Root Json)  
  5. {  
  6. InjuerisResult obj_result = new InjuerisResult();  
  7. try  
  8. {  
  9. if (Json == null)  
  10. {  
  11. obj_result.Response = 2;  
  12. obj_result.ErrorMessage = " object is null";  
  13. }  
  14. else  
  15. {  
  16. List<TblInjurie> list_Injurie = new List<TblInjurie>();  
  17. var JsonData = JsonConvert.SerializeObject((Json));  
  18. UserInjuries list = JsonConvert.DeserializeObject<UserInjuries>(JsonData);  
  19. foreach (var res in list.UserInjuriesList)------ Here I have face a issue (list.UserInjuriesList null)  
  20. {  
  21. var obj_tblInjurie = new TblInjurie();  
  22. obj_tblInjurie.Date = DateTime.ParseExact(res.Date, "d/M/yyyy", CultureInfo.InvariantCulture);  
  23. obj_tblInjurie.Description = res.Description;  
  24. obj_tblInjurie.UserInjuriesTypeId = res.UserInjuriesTypeId;  
  25. obj_tblInjurie.UserId = list.UserId;  
  26. obj_tblInjurie.IsActive = true;  
  27. obj_tblInjurie.UpdatedDate = DateTime.UtcNow;  
  28. list_Injurie.Add(obj_tblInjurie);  
  29. }  
  30. _context.TblInjurie.AddRange(list_Injurie);  
  31. _context.SaveChanges();  
  32. It's me modle code  
  33. [Table("TblInjurie")]  
  34. public class InjerisDetail  
  35. {  
  36. //public int? UserId { get; set; }  
  37. public int UserInjuriesTypeId { getset; }  
  38. public string Description { getset; }  
  39. public bool? IsActive { getset; }  
  40. [Column(TypeName = "date")]  
  41. public string Date { getset; }  
  42. }  
  43. public class UserInjuries  
  44. {  
  45. public int? UserId { getset; }  
  46. public List<InjerisDetail> UserInjuriesList { getset; }  
  47. }  
  48. public class Root  
  49. {  
  50. public UserInjuries userInjuries { getset; }  
  51. }  
Please resolve this issue it's important for me.

Answers (5)