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);
            }
  • 相关阅读:
    Nacos微服务部署(超详细)基于Centos7
    Centos7配置Mysql5.7数据库
    django搭建web (一)
    NetFPGA-1G-CML Demo --- reference_router_nf1_cml
    Linux下Java通用安装方法
    NetFPGA-1G-CML Demo --- openflow_switch
    原型设计(结对第一次)
    第二次作业——个人项目实战
    游戏
    python学习笔记-问题
  • 原文地址:https://www.cnblogs.com/neozhu/p/2281658.html
Copyright © 2011-2022 走看看