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);
            }
  • 相关阅读:
    电影观后感
    自定义内存管理
    web.xml配置详解
    Annotation
    Centos中yum方式安装java
    linux下添加用户并赋予root权限
    Injector
    Container
    GlassFish的安装与使用(Windows)
    Version Control
  • 原文地址:https://www.cnblogs.com/neozhu/p/2281658.html
Copyright © 2011-2022 走看看