EF生成类自动添加特性和注释

1.自动添加特性

修改Model.edmx文件下面的Model.tt文件的内容

1.1添加DataContract

 public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "[DataContract]\n\t{0} {1}partial class {2}{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
    }

 1.2 添加using System.Runtime.Serialization;

  public string UsingDirectives(bool inHeader, bool includeCollections = true)
    {
        return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
            ? string.Format(
                CultureInfo.InvariantCulture,
                "{0}using System;{1}" +
                "{2}"+"using System.Runtime.Serialization;",
                inHeader ? Environment.NewLine : "",
                includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
                inHeader ? "" : Environment.NewLine)
            : "";
    }

 1.3 添加DataMember

  public string Property(EdmProperty edmProperty)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "[DataMember]\n\t{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)));
    }

 Property下面的NavigationProperty不用修改,这样就不会导致循环了。需要将关联对象传递过去的话自己添加DataMember。

 

2 添加EF6.0 生成的代码中没有注释的解决方法

具体见链接:

https://blog.csdn.net/yangxinyue315/article/details/44600671

 

 

本文参考于:https://blog.csdn.net/llhswwha/article/details/79181684

 

你可能感兴趣的:(EF生成类自动添加特性和注释)