Answered by:
Need help with EF 4.5 .tt file that has the POCO entities

Question
-
I want to place two using directives in a EF 4.5 edmx .tt file and am having trouble. The 4.0 syntax was different and easier to manipulate.
Any help would be appreciated.... See the boldfaced lines for the items I want to add.
EF 4.0 syntax:
using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.ServiceModel;
4.5 syntax
public string UsingDirectives(bool inHeader, bool includeCollections = true) { return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) ? string.Format( CultureInfo.InvariantCulture, "{0}using System;{1}" + "{2}", inHeader ? Environment.NewLine : "", includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "", inHeader ? "" : Environment.NewLine) : ""; }
I need to add the following attributes in the .tt file:
EF 4.0 syntax
[Serializable] [DataContract(IsReference = true)] <#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#> { <#
4.5 syntax
public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} {1}partial class {2}{3}", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); }
4.0 syntax
void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility) { #> [DataMember] <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; } <#+ }
4.5 syntax
I can't find any similiar syntax
Bill Yeager
Friday, August 31, 2012 4:15 AM
Answers
-
Allen, thanks... That's partially what I am looking for, but someone at StackOverflow helped me out with everything I need in the template file.
Here is how to do it:
Usings: public string UsingDirectives(bool inHeader, bool includeCollections = true) { var usings = new List<string>() { "using System;", "using System.Runtime.Serialization;", "using System.ServiceModel;" }; if (includeCollections) { usings.Add("using System.Collections.Generic;"); } return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) ? string.Format( CultureInfo.InvariantCulture, "{0}{1}{2}", inHeader ? Environment.NewLine : "", String.Join(Environment.NewLine, usings), inHeader ? "" : Environment.NewLine) : ""; } Class: public string EntityClassOpening(EntityType entity) { const string attributes = "[Serializable, DataContract(IsReference = true)]"; return string.Format( CultureInfo.InvariantCulture, "{0}{1}{2} {3}partial class {4}{5}", attributes, Environment.NewLine, Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); } Properties: public string Property(EdmProperty edmProperty) { return string.Format( CultureInfo.InvariantCulture, "[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}", Accessibility.ForProperty(edmProperty), _typeMapper.GetTypeName(edmProperty.TypeUsage), _code.Escape(edmProperty), _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), _code.SpaceAfter(Accessibility.ForSetter(edmProperty))); } public string NavigationProperty(NavigationProperty navigationProperty) { var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType()); return string.Format( CultureInfo.InvariantCulture, "[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}", AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)), navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, _code.Escape(navigationProperty), _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)), _code.SpaceAfter(Accessibility.ForSetter(navigationProperty))); }
Bill Yeager
- Marked as answer by Bill_Yeager Tuesday, September 4, 2012 3:50 AM
Tuesday, September 4, 2012 3:50 AM
All replies
-
Basically, I want the output of the class when it's regenerated from the edmx file to look like this:
using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.ServiceModel; namespace YeagerTechModel { [Serializable] [DataContract(IsReference = true)] public partial class Priority { public Priority() { this.Projects = new HashSet<Project>(); } [DataMember] public short PriorityID { get; set; } [DataMember] public string Description { get; set; } [DataMember] public virtual ICollection<Project> Projects { get; set; } } }
Bill Yeager
Friday, August 31, 2012 4:24 AM -
Hi Bill Yeager,
Welcome to MSDN Forum.
I suggest you to use this template: ADO.NET C# DbContext Generator With WCF Support.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Monday, September 3, 2012 3:14 AM -
Allen, thanks... That's partially what I am looking for, but someone at StackOverflow helped me out with everything I need in the template file.
Here is how to do it:
Usings: public string UsingDirectives(bool inHeader, bool includeCollections = true) { var usings = new List<string>() { "using System;", "using System.Runtime.Serialization;", "using System.ServiceModel;" }; if (includeCollections) { usings.Add("using System.Collections.Generic;"); } return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) ? string.Format( CultureInfo.InvariantCulture, "{0}{1}{2}", inHeader ? Environment.NewLine : "", String.Join(Environment.NewLine, usings), inHeader ? "" : Environment.NewLine) : ""; } Class: public string EntityClassOpening(EntityType entity) { const string attributes = "[Serializable, DataContract(IsReference = true)]"; return string.Format( CultureInfo.InvariantCulture, "{0}{1}{2} {3}partial class {4}{5}", attributes, Environment.NewLine, Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); } Properties: public string Property(EdmProperty edmProperty) { return string.Format( CultureInfo.InvariantCulture, "[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}", Accessibility.ForProperty(edmProperty), _typeMapper.GetTypeName(edmProperty.TypeUsage), _code.Escape(edmProperty), _code.SpaceAfter(Accessibility.ForGetter(edmProperty)), _code.SpaceAfter(Accessibility.ForSetter(edmProperty))); } public string NavigationProperty(NavigationProperty navigationProperty) { var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType()); return string.Format( CultureInfo.InvariantCulture, "[DataMember] {0} {1} {2} {{ {3}get; {4}set; }}", AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)), navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, _code.Escape(navigationProperty), _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)), _code.SpaceAfter(Accessibility.ForSetter(navigationProperty))); }
Bill Yeager
- Marked as answer by Bill_Yeager Tuesday, September 4, 2012 3:50 AM
Tuesday, September 4, 2012 3:50 AM