Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. public class Order
  2. {
  3. [Key]
  4. public Guid Id { get; set; }
  5. [Required]
  6. public int Type { get; set; }
  7. [Required]
  8. public int Day { get; set; }
  9. public Guid? GroupId { get; set; }
  10. public Group Group { get; set; }
  11. public Guid? UserId{ get; set; }
  12. public User User { get; set; }
  13. }
  14. public class Author
  15. {
  16. [Key]
  17. [ForeignKey("User")]
  18. public Guid Id { get; set; }
  19. public virtual User User { get; set; }
  20. public virtual ICollection<Group> Groups { get; set; }
  21.  
  22. public Author()
  23. {
  24. Groups = new List<Group>();
  25. }
  26. }
  27. public class User
  28. {
  29. public User()
  30. {
  31. Groups = new List<Group>();
  32. }
  33. [Key]
  34. [ForeignKey("Author")]
  35. public Guid Id { get; set; }
  36. [Required]
  37. public string Login { get; set; }
  38. [Required]
  39. public DateTime BirthDay { get; set; }
  40. [Required]
  41. public string FirsName { get; set; }
  42. [Required]
  43. public string SecondName { get; set; }
  44. [Required]
  45. public string Password { get; set; }
  46. [Required]
  47. public string AvatarPath { get; set; }
  48. [Required]
  49. public string ResponseQuestion { get; set; }
  50. [Required]
  51. public string SecretQuestion { get; set; }
  52. public virtual ICollection<Group> Groups { get; set; }
  53. public virtual Author Author { get; set; }
  54.  
  55. public static implicit operator Author(User usr)
  56. {
  57. return new Author()
  58. {
  59. User = usr
  60. };
  61. }
  62. }
  63. public class Group
  64. {
  65. public Group()
  66. {
  67. this.Users = new List<User>();
  68. }
  69. [Key]
  70. public Guid Id { get; set; }
  71. public string Name { get; set; }
  72. public Guid? AuthorId{ get; set; }
  73. public virtual Author Author { get; set; }//Автор группы
  74. public virtual ICollection<User> Users { get; set; }
  75.  
  76. public Group(string name, Author author)
  77. {
  78. this.Name = name;
  79. this.Author = author;
  80. }
  81. }
  82. public class StoreDB : DbContext
  83. {
  84. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  85. {
  86. modelBuilder.Entity<User>()
  87. .HasOptional<Author>(a => a.Author)
  88. .WithRequired(a => a.User);
  89. }
  90.  
  91. public DbSet<User> Users { get; set; }
  92. public DbSet<Group> Groups { get; set; }
  93. public DbSet<Order> Orders { get; set; }
  94. public DbSet<Author> Authors { get; set; }
  95.  
  96. public User GetUser(string login)
  97. {
  98. return this.Users.FirstOrDefault((usr) => usr.Login == login);
  99. }
  100. public void AddUser(User usr)
  101. {
  102. this.Users.Add(usr);
  103. this.SaveChanges();
  104. }
  105. public void AddGroup(Group grp)
  106. {
  107. this.Groups.Add(grp);
  108. this.SaveChanges();
  109. }
  110. public bool IsContainUser(string login, string pass)
  111. {
  112. try
  113. {
  114. this.Users.First((u) => u.Login == login && u.Password == pass);
  115. return true;
  116. }
  117. catch
  118. {
  119. return false;
  120. }
  121. }
  122. public bool IsContainUser(string login)
  123. {
  124. try
  125. {
  126. this.Users.First((u) => u.Login == login);
  127. return true;
  128. }
  129. catch
  130. {
  131. return false;
  132. }
  133. }
  134. public bool ResponceOnQuestion(string login, string response)
  135. {
  136. try
  137. {
  138. this.Users.First(u => u.Login == login && u.ResponseQuestion == response);
  139. return true;
  140. }
  141. catch
  142. {
  143. return false;
  144. }
  145. }
  146. public void UpdatePassword(string login, string newPassword)
  147. {
  148. User updateUsr = this.Users.First(u => u.Login == login);
  149. updateUsr.Password = newPassword;
  150. this.Entry<User>(updateUsr).State = EntityState.Modified;
  151. this.SaveChanges();
  152. }
  153. }
  154.  
  155. <add name="StoreDB" connectionString="data source=(localdb)v11.0;Initial Catalog=ChudoPechkaStore;Integrated Security=True;"
  156. providerName="System.Data.SqlClient"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement