PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2010/CSEFCodeOnly/EntityConfigurations.cs

#
C# | 319 lines | 209 code | 32 blank | 78 comment | 7 complexity | 285b67d5dcede2caa8b6e8d75cc7ebf7 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: EntityConfigurations.cs
  3. * Project: CSEFCodeOnly
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * This code file contains the EntityConfiguration for the POCO entities to
  7. * create the Entity Data Model metadata.
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  14. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  16. \***************************************************************************/
  17. #region Using directive
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using Microsoft.Data.Objects;
  23. #endregion
  24. namespace CSEFCodeOnly
  25. {
  26. #region EntityConfiguration for TPT inheritance POCO entities
  27. public class PersonTPTConfiguration : EntityConfiguration<PersonTPT>
  28. {
  29. public PersonTPTConfiguration()
  30. {
  31. // Set the entity key
  32. HasKey(p => p.PersonID);
  33. // Set the table mapping of the TPT inheritance entities
  34. // The table name here is "PeopleTPT"
  35. // Anonymous type properties are mapped to the table column names
  36. MapHierarchy(
  37. p => new
  38. {
  39. p.PersonID,
  40. Name_FirstName = p.Name.FirstName,
  41. Name_LastName = p.Name.LastName,
  42. Address_City = p.Address.City,
  43. Address_Country = p.Address.Country,
  44. Address_Zipcode = p.Address.Zipcode
  45. }).ToTable("PeopleTPT");
  46. }
  47. }
  48. public class InstructorTPTConfiguration :
  49. EntityConfiguration<InstructorTPT>
  50. {
  51. public InstructorTPTConfiguration()
  52. {
  53. // Set the regular property column mapping
  54. Property(i => i.HireDate);
  55. // Set the many-to-many relationship between the Course and
  56. // InstructorTPT entities
  57. Relationship(i => i.Courses).FromProperty(c => c.Instructors);
  58. // Set the table mapping of the TPT inheritance entities
  59. // The table name here is "InstructorsTPT"
  60. // Anonymous type properties are mapped to the table column names
  61. MapHierarchy(
  62. i => new
  63. {
  64. i.PersonID,
  65. i.HireDate
  66. }).ToTable("InstructorsTPT");
  67. }
  68. }
  69. public class StudentTPTConfiguration : EntityConfiguration<StudentTPT>
  70. {
  71. public StudentTPTConfiguration()
  72. {
  73. // Set the regular property column mappings
  74. Property(s => s.EnrollmentDate);
  75. Property(s => s.Degree);
  76. Property(s => s.Credits);
  77. // Set the table mapping of the TPT inheritance entities
  78. // The table name here is "StudentsTPT"
  79. // Anonymous type properties are mapped to the table column names
  80. MapHierarchy(
  81. s => new
  82. {
  83. s.PersonID,
  84. s.EnrollmentDate,
  85. s.Degree,
  86. s.Credits
  87. }).ToTable("StudentsTPT");
  88. // Set the one-to-many relationship between the StudentTPT and
  89. // CourseStudent entities
  90. Relationship(s => s.CourseStudents)
  91. .FromProperty(cs => cs.Student);
  92. }
  93. }
  94. public class AdminTPTConfiguration : EntityConfiguration<AdminTPT>
  95. {
  96. public AdminTPTConfiguration()
  97. {
  98. // Set the regular property column mapping
  99. Property(a => a.AdminDate);
  100. // Set the table mapping of the TPT inheritance entities
  101. // The table name here is "AdminsTPT"
  102. // Use the EntityMap.Row and EntityMap.Column to map the
  103. // properties to the table columns
  104. MapHierarchy(
  105. a => EntityMap.Row(
  106. EntityMap.Column(a.PersonID, "PersonID"),
  107. EntityMap.Column(a.AdminDate, "AdminDate")
  108. )).ToTable("AdminsTPT");
  109. }
  110. }
  111. public class BusinessStudentTPTConfiguration :
  112. EntityConfiguration<BusinessStudentTPT>
  113. {
  114. public BusinessStudentTPTConfiguration()
  115. {
  116. // Set the regular property column mapping
  117. Property(bs => bs.BusinessCredits);
  118. // Set the table mapping of the TPT inheritance entities
  119. // The table name here is "BusinessStudentsTPT"
  120. // Use the EntityMap.Row and EntityMap.Column to map the
  121. // properties to the table columns
  122. MapHierarchy(
  123. bs => EntityMap.Row(
  124. EntityMap.Column(bs.PersonID, "PersonID"),
  125. EntityMap.Column(bs.BusinessCredits, "BusinessCredits")
  126. )).ToTable("BusinessStudentsTPT");
  127. }
  128. }
  129. #endregion
  130. #region EntityConfiguration for other relational POCO entities
  131. public class DepartmentConfiguration : EntityConfiguration<Department>
  132. {
  133. public DepartmentConfiguration()
  134. {
  135. // Set the entity key
  136. HasKey(d => d.DepartmentID);
  137. // Set the varchar typed column, max length: 100, non-nullable
  138. Property(d => d.Name).HasMaxLength(100).IsRequired();
  139. // Set the decimal typed column, precision: 18, scale: 0,
  140. // nullable
  141. Property(d => d.Budget).Precision = 18;
  142. Property(d => d.Budget).Scale = 0;
  143. // Set the regular property column mapping
  144. Property(d => d.StartDate);
  145. // Set the one-to-many relationship between the Department and
  146. // Course entities
  147. Relationship(d => d.Courses).FromProperty(c => c.Department);
  148. }
  149. }
  150. public class CourseConfiguration : EntityConfiguration<Course>
  151. {
  152. public CourseConfiguration()
  153. {
  154. // Set the entity key
  155. HasKey(c => c.CourseID);
  156. // Set the varchar typed column, max length: 100, non-nullable
  157. Property(c => c.Title).HasMaxLength(100).IsRequired();
  158. // Set the regular property column mapping
  159. Property(c => c.Credits);
  160. // Set the FK association between the Department and Course
  161. // entities, and set the FK association property as
  162. // Course.DepartmentID
  163. // IsRequired() indicates it is 1:* relationship instead of
  164. // 0..1:* relationship
  165. Relationship(c => c.Department).IsRequired()
  166. .FromProperty(d => d.Courses).HasConstraint(
  167. (c, d) => c.DepartmentID == d.DepartmentID);
  168. // Set the one-to-many relationship between the Course and
  169. // CourseStudent entities
  170. Relationship(c => c.CourseStudents)
  171. .FromProperty(cs => cs.Course);
  172. // Set the many-to-many relationship between the Course and
  173. // InstructorTPT entities
  174. Relationship(c => c.Instructors)
  175. .FromProperty(i => i.Courses);
  176. }
  177. }
  178. public class CourseStudentConfiguration :
  179. EntityConfiguration<CourseStudent>
  180. {
  181. public CourseStudentConfiguration()
  182. {
  183. // Set the composite entity key
  184. HasKey(cs => new { cs.PersonID, cs.CourseID });
  185. // Set the regular property column mappings
  186. Property(cs => cs.PersonID);
  187. Property(cs => cs.CourseID);
  188. Property(cs => cs.Score);
  189. // Set the FK association between the Course and CourseStudent
  190. // entities, and set the FK association property as
  191. // CourseStudent.CourseID
  192. Relationship(cs => cs.Course).IsRequired()
  193. .FromProperty(c => c.CourseStudents)
  194. .HasConstraint((cs, c) => cs.CourseID == c.CourseID);
  195. // Set the FK association between the StudentTPT and
  196. // CourseStudent entities, and set the FK association property
  197. // as CourseStudent.PersonID
  198. Relationship(cs => cs.Student).IsRequired()
  199. .FromProperty(s => s.CourseStudents)
  200. .HasConstraint((cs, s) => cs.PersonID == s.PersonID);
  201. }
  202. }
  203. #endregion
  204. #region EntityConfiguration for TPH inheritance POCO entities
  205. public class PersonTPHConfiguration : EntityConfiguration<PersonTPH>
  206. {
  207. public PersonTPHConfiguration()
  208. {
  209. // Set the entity key
  210. HasKey(p => p.PersonID);
  211. // Set the table mapping of the TPT inheritance entities
  212. // The table name here is "PeopleTPH"
  213. // Each type has its own property and column mappings
  214. // The base and each derived type have the discriminitor column
  215. // "PersonCategory"
  216. MapHierarchy()
  217. .Case<PersonTPH>(p => new
  218. {
  219. p.PersonID,
  220. p.Name.FirstName,
  221. p.Name.LastName,
  222. p.Address.City,
  223. p.Address.Country,
  224. p.Address.Zipcode,
  225. PersonCategory = 0
  226. })
  227. .Case<InstructorTPH>(i => new
  228. {
  229. i.PersonID,
  230. i.Name.FirstName,
  231. i.Name.LastName,
  232. i.Address.City,
  233. i.Address.Country,
  234. i.Address.Zipcode,
  235. i.HireDate,
  236. PersonCategory = 1
  237. })
  238. .Case<StudentTPH>(s => new
  239. {
  240. s.PersonID,
  241. s.Name.FirstName,
  242. s.Name.LastName,
  243. s.Address.City,
  244. s.Address.Country,
  245. s.Address.Zipcode,
  246. s.EnrollmentDate,
  247. PersonCategory = 2
  248. })
  249. .Case<AdminTPH>(a => new
  250. {
  251. a.PersonID,
  252. a.Name.FirstName,
  253. a.Name.LastName,
  254. a.Address.City,
  255. a.Address.Country,
  256. a.Address.Zipcode,
  257. a.AdminDate,
  258. PersonCategory = 3
  259. }).ToTable("PeopleTPH");
  260. }
  261. }
  262. #endregion
  263. #region ComplexTypeCinfiguration for Complex Type entities
  264. public class ComplexTypeNameConfiguration :
  265. ComplexTypeConfiguration<Name>
  266. {
  267. public ComplexTypeNameConfiguration()
  268. {
  269. // Set the two varchar typed column the max length and
  270. // non-nullable
  271. Property(n => n.FirstName).IsMax().IsRequired();
  272. Property(n => n.LastName).IsMax().IsRequired();
  273. }
  274. }
  275. public class ComplexTypeAddressConfiguration :
  276. ComplexTypeConfiguration<Address>
  277. {
  278. public ComplexTypeAddressConfiguration()
  279. {
  280. // Set the three varchar typed column the max length
  281. Property(a => a.City).IsMax();
  282. Property(a => a.Country).IsMax();
  283. Property(a => a.Zipcode).IsMax();
  284. }
  285. }
  286. #endregion
  287. }