Entity.tt 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <#@ template hostspecific="true" language="C#" #>
  2. <#@ include file="EF.Utility.CS.ttinclude" #><#@
  3. output extension=".cs" #><#
  4. var efHost = (EfTextTemplateHost)Host;
  5. var code = new CodeGenerationTools(this);
  6. #>
  7. using System;
  8. using System.Collections.Generic;
  9. namespace <#= code.EscapeNamespace(efHost.Namespace) #>
  10. {
  11. [Serializable]
  12. public partial class <#= efHost.EntityType.Name #>
  13. {
  14. <#
  15. var collectionNavigations = efHost.EntityType.NavigationProperties.Where(
  16. np => np.DeclaringType == efHost.EntityType
  17. && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
  18. // Add a ctor to initialize any collections
  19. if (collectionNavigations.Any())
  20. {
  21. #>
  22. public <#= code.Escape(efHost.EntityType) #>()
  23. {
  24. <#
  25. foreach (var navProperty in collectionNavigations)
  26. {
  27. #>
  28. this.<#= code.Escape(navProperty) #> = new List<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>>();
  29. <#
  30. }
  31. #>
  32. }
  33. <#
  34. }
  35. foreach (var property in efHost.EntityType.Properties)
  36. {
  37. var typeUsage = code.Escape(property.TypeUsage);
  38. // Fix-up spatial types for EF6
  39. if (efHost.EntityFrameworkVersion >= new Version(6, 0)
  40. && typeUsage.StartsWith("System.Data.Spatial."))
  41. {
  42. typeUsage = typeUsage.Replace(
  43. "System.Data.Spatial.",
  44. "System.Data.Entity.Spatial.");
  45. }
  46. #>
  47. <#= Accessibility.ForProperty(property) #> <#= typeUsage #> <#= code.Escape(property) #> { get; set; }
  48. <#
  49. }
  50. foreach (var navProperty in efHost.EntityType.NavigationProperties.Where(np => np.DeclaringType == efHost.EntityType))
  51. {
  52. if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
  53. {
  54. #>
  55. public virtual ICollection<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Escape(navProperty) #> { get; set; }
  56. <#
  57. }
  58. else
  59. {
  60. #>
  61. public virtual <#= code.Escape(navProperty.ToEndMember.GetEntityType()) #> <#= code.Escape(navProperty) #> { get; set; }
  62. <#
  63. }
  64. }
  65. #>
  66. }
  67. }