Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. modelSelfRefferenceCS
  2. namespace Lab_2
  3. {
  4. using System;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8. using System.ComponentModel.DataAnnotations;
  9. using System.ComponentModel.DataAnnotations.Schema;
  10. public class ModelSelfReferences : DbContext
  11. {
  12. public ModelSelfReferences()
  13. : base("name=ModelSelfReferences")
  14. {
  15. }
  16. // public virtual DbSet<MyEntity> MyEntities { get; set; }
  17. public virtual DbSet<SelfReference> SelfReferences { get; set; }
  18. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  19. {
  20. base.OnModelCreating(modelBuilder);
  21. modelBuilder.Entity<SelfReference>()
  22. .HasMany(m => m.References)
  23. .WithOptional(m => m.ParentSelfReference);
  24. }
  25. }
  26.  
  27. public class ProductContext : DbContext
  28. {
  29. public DbSet<Product> Products { get; set; }
  30. public ProductContext() : base("name=ProductContext")
  31. { }
  32. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  33. {
  34. base.OnModelCreating(modelBuilder);
  35. modelBuilder.Entity<Product>()
  36. .Map(m =>
  37. {
  38. m.Properties(p => new { p.SKU, p.Description, p.Price });
  39. m.ToTable("Product", "BazaDeDate");
  40. })
  41. .Map(m =>
  42. {
  43. m.Properties(p => new { p.SKU, p.ImageURL });
  44. m.ToTable("ProductWebInfo", "BazaDeDate");
  45. });
  46. }
  47. }
  48.  
  49. public class PhotographContext : DbContext
  50. {
  51. public DbSet<Photograph> Photographs { get; set; }
  52. public PhotographContext() : base("name=PhotographContext")
  53. { }
  54. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  55. {
  56. base.OnModelCreating(modelBuilder);
  57. modelBuilder.Entity<Photograph>()
  58. .HasRequired(p => p.PhotographFullImage)
  59. .WithRequiredPrincipal(p => p.Photograph);
  60. modelBuilder.Entity<Photograph>().ToTable("Photograph",
  61. "BazaDeDate");
  62. modelBuilder.Entity<PhotographFullImage>().
  63. ToTable("Photograph", "BazaDeDate");
  64. }
  65.  
  66. }
  67.  
  68. public class BusinessContext : DbContext
  69. {
  70. public DbSet<Business> Businesses { get; set; }
  71. public BusinessContext() : base("name=BusinessContext")
  72. { }
  73. }
  74.  
  75. public class EmployeeContext : DbContext
  76. {
  77. public DbSet<Employee> Employees { get; set; }
  78. public EmployeeContext() : base("name=EmployeeContext")
  79. { }
  80. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  81. {
  82. base.OnModelCreating(modelBuilder);
  83. modelBuilder.Entity<Employee>()
  84. .Map<FullTimeEmployee>(m =>
  85. m.Requires("EmployeeType").HasValue(1))
  86. .Map<HourlyEmployee>(m =>
  87. m.Requires("EmployeeType").HasValue(2));
  88. }
  89.  
  90. }
  91.  
  92. public class PhotographFullImage
  93. {
  94. [Key]
  95. public int PhotoId { get; set; }
  96. public byte[] HighResolutionBits { get; set; }
  97. [ForeignKey("PhotoId")]
  98. public virtual Photograph Photograph { get; set; }
  99. }
  100.  
  101.  
  102. //public class MyEntity
  103. //{
  104. // public int Id { get; set; }
  105. // public string Name { get; set; }
  106. //}
  107. public class SelfReference
  108. {
  109. [Key]
  110. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  111. public int SelfReferenceId { get; private set; }
  112. public string Name { get; set; }
  113. public int? ParentSelfReferenceId { get; private set; }
  114. [ForeignKey("ParentSelfReferenceId")]
  115. public SelfReference ParentSelfReference { get; set; }
  116. public virtual ICollection<SelfReference> References { get; set; }
  117. public SelfReference()
  118. {
  119. References = new HashSet<SelfReference>();
  120. }
  121. }
  122.  
  123. public class Product
  124. {
  125. [Key]
  126. [DatabaseGenerated(DatabaseGeneratedOption.None)]
  127. public int SKU { get; set; }
  128. public string Description { get; set; }
  129. public decimal Price { get; set; }
  130. public string ImageURL { get; set; }
  131. }
  132.  
  133. public class Photograph
  134. {
  135. [Key]
  136. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  137. public int PhotoId { get; set; }
  138. public string Title { get; set; }
  139. public byte[] ThumbnailBits { get; set; }
  140. [ForeignKey("PhotoId")]
  141. public virtual PhotographFullImage PhotographFullImage { get; set; }
  142. }
  143.  
  144. //scenario 4
  145. [Table("Business", Schema = "BazaDeDate")]
  146. public class Business
  147. {
  148. [Key]
  149. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  150. public int BusinessId { get; protected set; }
  151. public string Name { get; set; }
  152. public string LicenseNumber { get; set; }
  153. }
  154.  
  155. [Table("eCommerce", Schema = "BazaDeDate")]
  156. public class eCommerce : Business
  157. {
  158. public string URL { get; set; }
  159. }
  160. [Table("Retail", Schema = "BazaDeDate")]
  161. public class Retail : Business
  162. {
  163. public string Address { get; set; }
  164. public string City { get; set; }
  165. public string State { get; set; }
  166. public string ZIPCode { get; set; }
  167. }
  168.  
  169.  
  170. //scenario5
  171. public abstract class Employee
  172. {
  173. [Key]
  174. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  175. public int EmployeeId { get; protected set; }
  176. public string FirstName { get; set; }
  177. public string LastName { get; set; }
  178. }
  179.  
  180. public class FullTimeEmployee : Employee
  181. {
  182. public decimal? Salary { get; set; }
  183. }
  184. public class HourlyEmployee : Employee
  185. {
  186. public decimal? Wage { get; set; }
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement