zoukankan      html  css  js  c++  java
  • EF自动生成的(T4模板) 关联属性元数据修改

    为了实现 T4模板关联属性 不要序列化的问题 就是要在具体的 关联属性上面添加一个元数据

    这里利用以前的 Newtonsoft.Json 这个框架为例

    效果应该为

    image

    就是要在关联属性上面添加元数据  【JsonIgnore】

    注意: 默认会报错,如下

    image

    请记住错误个数,多了就证明你写的有问题。而且你不要奇怪有这么多错误。

    如果你不出现这么多个错误提示,你就应该去找一下模板 如下的位置。

    image

    把T4模板 关掉错误就不会存在了。

     

     

    步骤

    1、导入命名空间。不然出来的全是BUG

    //添加引用
    public string UsingDirectives(bool inHeader, bool includeCollections = true)
    {
    return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
    ? string.Format(
    CultureInfo.InvariantCulture,
    "{0}using System;{1}{3}" +
    "{2}",
    inHeader ? Environment.NewLine : "",
    includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
    inHeader ? "" : Environment.NewLine, Environment.NewLine + "using Newtonsoft.Json;")
    : "";
    }

    2、导入命名空间  ,然后我们就要找导航属性到底是什么了

    首先我找到 Property 属性这里,

    image

    加到里面后发现效果有问题。

    除了导航属性外 ,其他的属性都有了元数据,就是说我找错位置了。

    然后我继续找 在   Property  下面有一个 NavigationProperty

    public string NavigationProperty(NavigationProperty navigationProperty)
       {
           var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
           return string.Format(
               CultureInfo.InvariantCulture,
               "{5}{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)), "[JsonIgnore]" + Environment.NewLine);
       }

    3、果然这样就有了数据

    保存后会自动修改元数据,所以不用担心。

    4、扩展:
    下面 提供一些其他的特性添加的位置

    如果添加下面这些数据,还要导入命名空间

    using System.Runtime.Serialization; 方法同步骤1

    //给属性增加[DataMember]
    public string Property(EdmProperty edmProperty)
    {
    return string.Format(
    CultureInfo.InvariantCulture,
    "{5}{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)), "[DataMember]" + Environment.NewLine);
    }
    //给类添加[DataContract]
    public string EntityClassOpening(EntityType entity)
    {
    return string.Format(
    CultureInfo.InvariantCulture,
    "{4}{0} {1}partial class {2}{3}",
    Accessibility.ForType(entity),
    _code.SpaceAfter(_code.AbstractOption(entity)),
    _code.Escape(entity),
    _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)),
    "[DataContract]" + Environment.NewLine
    );
    }

    越学越不懂。

  • 相关阅读:
    关于binary log一点总结[转]
    使用mysql索引技巧及注意事项
    优化php性能的一点总结
    html静态页面实现微信分享思路
    MySql字符串函数使用技巧
    Oracle计算时间差函数
    oracle10g获取Date类型字段无时分秒解决办法!
    Oracle常用函数
    COALESCE操作符
    关于null的操作
  • 原文地址:https://www.cnblogs.com/jixinyu12345/p/4863860.html
Copyright © 2011-2022 走看看