Entity.tt 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. using Newtonsoft.Json;
  10. namespace <#= code.EscapeNamespace(efHost.Namespace) #>
  11. {
  12. [Serializable]
  13. public partial class <#= efHost.EntityType.Name #>
  14. {
  15. <#
  16. var collectionNavigations = efHost.EntityType.NavigationProperties.Where(
  17. np => np.DeclaringType == efHost.EntityType
  18. && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
  19. // Add a ctor to initialize any collections
  20. if (collectionNavigations.Any())
  21. {
  22. #>
  23. public <#= code.Escape(efHost.EntityType) #>()
  24. {
  25. <#
  26. foreach (var navProperty in collectionNavigations)
  27. {
  28. #>
  29. this.<#= code.Escape(navProperty) #> = new List<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>>();
  30. <#
  31. }
  32. #>
  33. }
  34. <#
  35. }
  36. foreach (var property in efHost.EntityType.Properties)
  37. {
  38. var typeUsage = code.Escape(property.TypeUsage);
  39. // Fix-up spatial types for EF6
  40. if (efHost.EntityFrameworkVersion >= new Version(6, 0)
  41. && typeUsage.StartsWith("System.Data.Spatial."))
  42. {
  43. typeUsage = typeUsage.Replace(
  44. "System.Data.Spatial.",
  45. "System.Data.Entity.Spatial.");
  46. }
  47. #>
  48. <#= Accessibility.ForProperty(property) #> <#= typeUsage #> <#= code.Escape(property) #> { get; set; }
  49. <#
  50. }
  51. foreach (var navProperty in efHost.EntityType.NavigationProperties.Where(np => np.DeclaringType == efHost.EntityType))
  52. {
  53. if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
  54. {
  55. #>
  56. [JsonIgnore()]
  57. public virtual ICollection<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Escape(navProperty) #> { get; set; }
  58. <#
  59. }
  60. else
  61. {
  62. #>
  63. [JsonIgnore()]
  64. public virtual <#= code.Escape(navProperty.ToEndMember.GetEntityType()) #> <#= code.Escape(navProperty) #> { get; set; }
  65. <#
  66. }
  67. }
  68. #>
  69. }
  70. }