zoukankan      html  css  js  c++  java
  • make Entity Framework revert empty strings to null!

     public override int SaveChanges(SaveOptions options)
            {
                foreach (EntityObject entity in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified).Select(entry => entry.Entity))
                {
                    //if (entity == null || entity is User) continue;

                    string str = typeof(string).Name;

                    var props = entity.GetType().GetProperties();

                    var properties = from p in entity.GetType().GetProperties()
                                     where
                                     p.PropertyType.Name == str &&
                                     p.IsDefined(typeof(EdmScalarPropertyAttribute), false) &&
                                     p.IsDefined(typeof(DataMemberAttribute), false)
                                     select p;

                    foreach (var item in properties)
                    {
                        string value = (string)item.GetValue(entity, null);
                        if (value != null && value.Trim().Length == 0)
                            entity.GetType().GetField("_" + item.Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(entity, null);
                    }
                }
                return base.SaveChanges(options);
            }
  • 相关阅读:
    集群、分布式与微服务概念和区别理解
    博弈论的入门——nim游戏&&sg函数浅谈
    csp-2020 初赛游记
    洛谷 P2340 [USACO03FALL]Cow Exhibition G 题解
    P5687 [CSP-SJX2019]网格图 题解
    HBase 数据迁移/备份方法
    mac远程连接服务上传下载命令实例
    Redis安装详细步骤
    VMware虚拟机中的CentOS服务安装Nginx后本机无法访问的解决办法
    开发业务逻辑处理之策略模式场景使用
  • 原文地址:https://www.cnblogs.com/neozhu/p/2281658.html
Copyright © 2011-2022 走看看