Guest User

Untitled

a guest
Feb 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. public class User : IValidatableObject
  2. {
  3. public string Name{get;set;}
  4.  
  5. [Required]
  6. public DateTime CreationDate{get;set;}
  7.  
  8. public DateTime UpdatedOnDate{get;set;}
  9.  
  10. public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
  11. {
  12. if(Name="abc")
  13. {
  14. yield return new ValidationResult("please choose any other name then abc", new[] { "Name" });
  15. }
  16. }
  17. }
  18.  
  19. User u = new User();
  20. u.Name = "Some name";
  21. u.CreationDate = DateTime.Now
  22. dbContext.Users.Add(u);
  23. dbContext.SaveChanges();
  24.  
  25. // class structure that I am looking for
  26. public class User : IValidatableObject,IMyCustomInterFace
  27. {
  28. //rest codes as above class
  29.  
  30. public void MyMethod(Whatever)
  31. {
  32. //this method gets called after Validate() and before save
  33.  
  34. if(dataContext.Entry<User>(this).State == System.Data.EntityState.Added)
  35. {
  36. //add creation date_time
  37. this.CreationDate=DateTime.Now;
  38.  
  39. //SET MORE DEFAULTS
  40. }
  41.  
  42. if(dataContext.Entry<User>(this).State == System.Data.EntityState.Modified)
  43. {
  44. //update Updation time
  45. this.UpdatedOnDate=DateTime.Now;
  46. }
  47. }
  48. }
  49.  
  50. User u = new User();
  51. u.Name = "Some name";
  52. dbContext.Users.Add(u);
  53. dbContext.SaveChanges();
  54.  
  55. User u = getUserFromSomeWhere();
  56. u.Name = "Updated Name";
  57. dataContext.Entry<User>(u).State = System.Data.EntityState.Modified;
  58. dbContext.SaveChanges();
  59.  
  60. public interface IHasTimeStamp
  61. {
  62. void DoTimeStamp();
  63. }
  64.  
  65. Public class User : IHasTimeStamp
  66. (
  67. public void DoTimeStamp()
  68. {
  69. if(dataContext.Entry<User>(this).State == System.Data.EntityState.Added)
  70. {
  71. //add creation date_time
  72. this.CreationDate=DateTime.Now;
  73. }
  74.  
  75. if(dataContext.Entry<User>(this).State == System.Data.EntityState.Modified)
  76. {
  77. //update Updation time
  78. this.UpdatedOnDate=DateTime.Now;
  79. }
  80. }
  81. }
  82.  
  83. public partial class MyEntities
  84. {
  85. partial void OnContextCreated()
  86. {
  87. // Register the handler for the SavingChanges event.
  88. this.SavingChanges
  89. += new EventHandler(context_SavingChanges);
  90. }
  91.  
  92. // SavingChanges event handler.
  93. private static void context_SavingChanges(object sender, EventArgs e)
  94. {
  95. // Validate the state of each entity in the context
  96. // before SaveChanges can succeed.
  97. foreach (ObjectStateEntry entry in
  98. ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
  99. {
  100. if (!entry.IsRelationship && (entry.Entity is IHasTimeStamp))
  101. {
  102. (entry.Entity as IHasTimeStamp).DoTimeStamp();
  103. }
  104. }
  105. }
  106. }
Add Comment
Please, Sign In to add comment