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);
            }
  • 相关阅读:
    获取 鼠标 键盘 最后响应信息
    EF 6 for mysql
    Python中的OS模块。
    python中的随机函数random的用法示例random
    Python中OSI七层模型
    Pythoon中的面向对象三大特征
    Python中的函数(学习日记)
    Python的6种运算符(日记)
    计算机的发展史
    osi七层
  • 原文地址:https://www.cnblogs.com/neozhu/p/2281658.html
Copyright © 2011-2022 走看看