谈谈c# partial class的实际巧妙场景

partial class就是将一个类的代码内容放到任意地方可以多份,看上去鸡肋实际上还是有点用处的
假设有一个类 是自动生成的 ,比如上一篇文章的脚手架, 是根据数据库自动生成,而且将来加数据我可能需要再次自动生成 ,会覆盖数据,但是这个类有些字段我不需要在数据库里面映射,但是返回给前端序列化则需要,那么怎么办呢?
新建一个cs文件,放在其他地方 但是 里面的命名空间和类名一致partial修饰,完美。

namespace CoreWebApi_.Models
{
    public partial class Node
    {
        public List<View> view3ds { get; set; }

        public string? Name { get; set; }
        public string? Descript { get; set; }
        public string? Tag { get; set; }
     
        public int Id { get; set; }

        public int ParentId { get; set; }
        public int? Flag { get; set; }


    }
}

另外一部分非自动生成

using Newtonsoft.Json;

namespace CoreWebApi_.Models
{
    public partial class Node
    {

        public string? ItemName { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        public string? ItemDescript { get; set; }

        [JsonIgnore]

        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        public List<Node> items;
        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        public List<View> views;
        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        [JsonIgnore]
        public Node parentNode;
        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        List<View> view3Ds;

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值