Created
November 15, 2013 22:47
-
-
Save mikecole/7493017 to your computer and use it in GitHub Desktop.
Reverse Engineer Code First Entity template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template hostspecific="true" language="C#" #> | |
<#@ include file="EF.Utility.CS.ttinclude" #><#@ | |
output extension=".cs" #><# | |
var efHost = (EfTextTemplateHost)Host; | |
var code = new CodeGenerationTools(this); | |
#> | |
using System; | |
using System.Collections.Generic; | |
namespace <#= code.EscapeNamespace(efHost.Namespace) #> | |
{ | |
public partial class <#= efHost.EntityType.Name #> | |
{ | |
<# | |
var collectionNavigations = efHost.EntityType.NavigationProperties.Where( | |
np => np.DeclaringType == efHost.EntityType | |
&& np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many); | |
// Add a ctor to initialize any collections | |
if (collectionNavigations.Any()) | |
{ | |
#> | |
public <#= code.Escape(efHost.EntityType) #>() | |
{ | |
<# | |
foreach (var navProperty in collectionNavigations) | |
{ | |
#> | |
this.<#= code.Escape(navProperty) #> = new List<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>>(); | |
<# | |
} | |
#> | |
} | |
<# | |
} | |
foreach (var property in efHost.EntityType.Properties) | |
{ | |
var typeUsage = code.Escape(property.TypeUsage); | |
// Fix-up spatial types for EF6 | |
if (efHost.EntityFrameworkVersion >= new Version(6, 0) | |
&& typeUsage.StartsWith("System.Data.Spatial.")) | |
{ | |
typeUsage = typeUsage.Replace( | |
"System.Data.Spatial.", | |
"System.Data.Entity.Spatial."); | |
} | |
#> | |
<#= Accessibility.ForProperty(property) #> <#= typeUsage #> <#= code.Escape(property) #> { get; set; } | |
<# | |
} | |
foreach (var navProperty in efHost.EntityType.NavigationProperties.Where(np => np.DeclaringType == efHost.EntityType)) | |
{ | |
if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) | |
{ | |
#> | |
public virtual ICollection<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Escape(navProperty) #> { get; set; } | |
<# | |
} | |
else | |
{ | |
#> | |
public virtual <#= code.Escape(navProperty.ToEndMember.GetEntityType()) #> <#= code.Escape(navProperty) #> { get; set; } | |
<# | |
} | |
} | |
#> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment