IT:AD:EF/CodeFirst/Entities/AutoGeneration
Summary
Proccess
- Install the latest version of the
Entity Framework Power Tool
- Restart Visual Studio.
- On you Entities assembly:
- right click and select
Entity Framework/Customize Reverse Engineer Templates
* 3 templates will be added. - Save the project.
- TO the
model.tt
template, add:- virtual keyword to the properties to allow for
POCO
self-tracking - Add WCF
DataContract
andDataMember
to the properties
- Right click on the Project and select
Entity Framework/Reverse Engineer Code First
Note: For Enums, consider: http://linqto.net/blog/category/code-first/
Note:
I could not get the namespace to taken into account right-clicking entities.tt
and setting 'Custom Tool Namespace'.
I tried several options that all caused exceptions:
//namespace <#=code.VsNamespaceSuggestion();#> //namespace <#= System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint") #> namespace <#= code.EscapeNamespace(efHost.Namespace) #>
<#@ 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; using System.Runtime.Serialization; namespace <#= code.EscapeNamespace(efHost.Namespace) #> { [DataContract(Name="<#= efHost.EntityType.Name #>")] 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."); } #> /// <summary>Gets or sets the <#= code.Escape(property) #></summary> [DataMember(Name="<#= code.Escape(property) #>")] <#= Accessibility.ForProperty(property) #> virtual <#= 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) { #> /// <summary>Gets or sets the collection of <cref="<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>"/> associated to this entity.</summary> [DataMember(Name="<#= code.Escape(navProperty) #>")] public virtual ICollection<<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Escape(navProperty) #> { get; set; } <# } else { #> /// <summary>Gets or sets the <see cref="<#= code.Escape(navProperty.ToEndMember.GetEntityType()) #>"/> associated to this entity.</summary> [DataMember(Name="<#= code.Escape(navProperty) #>")] public virtual <#= code.Escape(navProperty.ToEndMember.GetEntityType()) #> <#= code.Escape(navProperty) #> { get; set; } <# } } #> } }