Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. private string name;
  2. private int minStock;
  3. private Category category;
  4. public Product()
  5. {
  6. Category = new Category();
  7. User = new User();
  8. }
  9.  
  10.  
  11. public int ProductId { get; set; }
  12. [Required]
  13. public string Name
  14. {
  15. get { return name; }
  16. set
  17. {
  18. name = value;
  19. OnPropertyChanged();
  20. }
  21. }
  22. [Required]
  23. public decimal SellPrice { get; set; }
  24. [Required]
  25. public int MinStock
  26. {
  27. get { return minStock; }
  28. set
  29. {
  30. minStock = value;
  31. OnPropertyChanged();
  32. }
  33. }
  34. public int Remove { get; set; }
  35. [Timestamp]
  36. public byte[] RowVersion { get; set; }
  37.  
  38. #region Relationships
  39. public virtual Measurement Mesurement { get; set; }
  40. public virtual Category Category
  41. {
  42. get { return category; }
  43. set
  44. {
  45. category = value;
  46. OnPropertyChanged();
  47. }
  48. }
  49. public virtual User User { get; set; }
  50. #endregion
  51.  
  52. using (MFSContext context = new MFSContext())
  53. {
  54. Product product = context.Products.Find(SelectedProduct.Id);
  55.  
  56. product.Name = "Frank";
  57.  
  58. context.Entry(product).State = EntityState.Modified;
  59.  
  60.  
  61. try
  62. {
  63. context.SaveChanges();
  64. }
  65. catch (DbEntityValidationException dbEx)
  66. {
  67. Exception raise = dbEx;
  68. foreach (var validationErrors in dbEx.EntityValidationErrors)
  69. {
  70. foreach (var validationError in validationErrors.ValidationErrors)
  71. {
  72. string message = string.Format("{0}:{1}",
  73. validationErrors.Entry.Entity.ToString(),
  74. validationError.ErrorMessage);
  75. // raise a new exception nesting
  76. // the current instance as InnerException
  77. raise = new InvalidOperationException(message, raise);
  78. }
  79. }
  80. throw raise;
  81. }
  82.  
  83. context.SaveChanges();
  84. LoadCategories();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement