1

我想在 EF 4.5 edmx .tt 文件中放置两个 using 指令,但遇到了麻烦。4.0 的语法不同并且更容易操作。

任何帮助将不胜感激.... EF 4.0 语法:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;

4.5 语法

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)
            : "";
    }

我需要在 .tt 文件中添加以下属性:

EF 4.0 语法

[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 语法

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 语法

void WriteProperty(string accessibility, string type, string name, string getterAccessibility, string setterAccessibility)
{
#> [DataMember]
    <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; }
<#+
}

4.5 语法我找不到任何类似的语法**

基本上,我希望从 edmx 文件重新生成类时的输出如下所示:

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; }
    }

}
4

1 回答 1

3

看不懂代码不知道怎么改,你是认真的吗?它只是一个字符串格式……即使您不了解格式,您仍然可以像在以前的版本中那样直接更新模板,因此您甚至不了解文本模板的工作原理。你真的应该花一些时间在基础教程上。

这里有一些例子

用途:

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)
        : "";
}

班级:

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)));
}

特性:

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)));
}
于 2012-08-31T18:36:43.523 回答