Beispiel #1
0
        private void DoEditPrepare(bool updateAllFields)
        {
            if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco)))
            {
                BasePoco ent = Entity as BasePoco;
                if (ent.UpdateTime == null)
                {
                    ent.UpdateTime = DateTime.Now;
                }
                if (string.IsNullOrEmpty(ent.UpdateBy))
                {
                    ent.UpdateBy = LoginUserInfo?.ITCode;
                }
            }
            var pros = typeof(TModel).GetProperties();

            #region 更新子表
            foreach (var pro in pros)
            {
                //找到类型为List<xxx>的字段
                if (pro.PropertyType.GenericTypeArguments.Count() > 0)
                {
                    //获取xxx的类型
                    var ftype = pro.PropertyType.GenericTypeArguments.First();
                    //如果xxx继承自TopBasePoco
                    if (ftype.IsSubclassOf(typeof(TopBasePoco)))
                    {
                        //界面传过来的子表数据

                        if (pro.GetValue(Entity) is IEnumerable <TopBasePoco> list && list.Count() > 0)
                        {
                            //获取外键字段名称
                            string         fkname   = DC.GetFKName <TModel>(pro.Name);
                            PropertyInfo[] itemPros = ftype.GetProperties();

                            bool found = false;
                            foreach (var newitem in list)
                            {
                                var subtype = newitem.GetType();
                                if (subtype.IsSubclassOf(typeof(BasePoco)))
                                {
                                    BasePoco ent = newitem as BasePoco;
                                    if (ent.UpdateTime == null)
                                    {
                                        ent.UpdateTime = DateTime.Now;
                                    }
                                    if (string.IsNullOrEmpty(ent.UpdateBy))
                                    {
                                        ent.UpdateBy = LoginUserInfo?.ITCode;
                                    }
                                }
                                //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(newitem, null);
                                    }
                                    if (!string.IsNullOrEmpty(fkname))
                                    {
                                        if (itempro.Name.ToLower() == fkname.ToLower())
                                        {
                                            itempro.SetValue(newitem, Entity.GetID());
                                            found = true;
                                        }
                                    }
                                }
                            }
                            //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环
                            if (found == false)
                            {
                                continue;
                            }


                            TModel _entity = null;
                            //打开新的数据库联接,获取数据库中的主表和子表数据
                            using (var ndc = DC.CreateNew())
                            {
                                _entity = ndc.Set <TModel>().Include(pro.Name).AsNoTracking().CheckID(Entity.GetID()).FirstOrDefault();
                            }
                            //比较子表原数据和新数据的区别
                            IEnumerable <TopBasePoco> toadd    = null;
                            IEnumerable <TopBasePoco> toremove = null;
                            IEnumerable <TopBasePoco> data     = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>;
                            Utils.CheckDifference(data, list, out toremove, out toadd);
                            //设定子表应该更新的字段
                            List <string> setnames = new List <string>();
                            foreach (var field in FC.Keys)
                            {
                                if (field.StartsWith("Entity." + pro.Name + "[0]."))
                                {
                                    string name = field.Replace("Entity." + pro.Name + "[0].", "");
                                    setnames.Add(name);
                                }
                            }

                            //前台传过来的数据
                            foreach (var newitem in list)
                            {
                                //数据库中的数据
                                foreach (var item in data)
                                {
                                    //需要更新的数据
                                    if (newitem.GetID().ToString() == item.GetID().ToString())
                                    {
                                        dynamic i           = newitem;
                                        var     newitemType = item.GetType();
                                        foreach (var itempro in itemPros)
                                        {
                                            if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && (updateAllFields == true || setnames.Contains(itempro.Name)))
                                            {
                                                var notmapped = itempro.GetCustomAttribute <NotMappedAttribute>();
                                                if (itempro.Name != "ID" && notmapped == null && itempro.PropertyType.IsList() == false)
                                                {
                                                    DC.UpdateProperty(i, itempro.Name);
                                                }
                                            }
                                        }
                                        if (item.GetType().IsSubclassOf(typeof(BasePoco)))
                                        {
                                            DC.UpdateProperty(i, "UpdateTime");
                                            DC.UpdateProperty(i, "UpdateBy");
                                        }
                                    }
                                }
                            }
                            //需要删除的数据
                            foreach (var item in toremove)
                            {
                                //如果是PersistPoco,则把IsValid设为false,并不进行物理删除
                                if (ftype.IsSubclassOf(typeof(PersistPoco)))
                                {
                                    (item as PersistPoco).IsValid    = false;
                                    (item as PersistPoco).UpdateTime = DateTime.Now;
                                    (item as PersistPoco).UpdateBy   = LoginUserInfo?.ITCode;
                                    dynamic i = item;
                                    DC.UpdateEntity(i);
                                }
                                else
                                {
                                    foreach (var itempro in itemPros)
                                    {
                                        if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                        {
                                            itempro.SetValue(item, null);
                                        }
                                    }
                                    dynamic i = item;
                                    DC.DeleteEntity(i);
                                }
                            }
                            //需要添加的数据
                            foreach (var item in toadd)
                            {
                                if (item.GetType().IsSubclassOf(typeof(BasePoco)))
                                {
                                    BasePoco ent = item as BasePoco;
                                    if (ent.CreateTime == null)
                                    {
                                        ent.CreateTime = DateTime.Now;
                                    }
                                    if (string.IsNullOrEmpty(ent.CreateBy))
                                    {
                                        ent.CreateBy = LoginUserInfo?.ITCode;
                                    }
                                }
                                DC.AddEntity(item);
                            }
                        }
                        else if (FC.Keys.Contains("Entity." + pro.Name + ".DONOTUSECLEAR") || (FC.ContainsKey("Entity." + pro.Name) && pro.GetValue(Entity) is IEnumerable <TopBasePoco> list2 && list2?.Count() == 0))
                        {
                            PropertyInfo[] itemPros = ftype.GetProperties();
                            var            _entity  = DC.Set <TModel>().Include(pro.Name).AsNoTracking().CheckID(Entity.GetID()).FirstOrDefault();
                            if (_entity != null)
                            {
                                IEnumerable <TopBasePoco> removeData = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>;
                                //如果是PersistPoco,则把IsValid设为false,并不进行物理删除
                                if (removeData is IEnumerable <PersistPoco> removePersistPocoData)
                                {
                                    foreach (var item in removePersistPocoData)
                                    {
                                        (item as PersistPoco).IsValid    = false;
                                        (item as PersistPoco).UpdateTime = DateTime.Now;
                                        (item as PersistPoco).UpdateBy   = LoginUserInfo?.ITCode;
                                        dynamic i = item;
                                        DC.UpdateEntity(i);
                                    }
                                }
                                else
                                {
                                    foreach (var item in removeData)
                                    {
                                        foreach (var itempro in itemPros)
                                        {
                                            if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                            {
                                                itempro.SetValue(item, null);
                                            }
                                        }
                                        dynamic i = item;
                                        DC.DeleteEntity(i);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion


            if (updateAllFields == false)
            {
                foreach (var field in FC.Keys)
                {
                    if (field.StartsWith("Entity.") && !field.Contains("["))
                    {
                        string name = field.Replace("Entity.", "");
                        try
                        {
                            DC.UpdateProperty(Entity, name);
                        }
                        catch (Exception ea)
                        {
                        }
                    }
                }
                if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco)))
                {
                    try
                    {
                        DC.UpdateProperty(Entity, "UpdateTime");
                        DC.UpdateProperty(Entity, "UpdateBy");
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                DC.UpdateEntity(Entity);
            }
        }
Beispiel #2
0
        private void DoAddPrepare()
        {
            var pros = typeof(TModel).GetProperties();

            //将所有TopBasePoco的属性赋空值,防止添加关联的重复内容
            if (typeof(TModel) != typeof(FileAttachment))
            {
                foreach (var pro in pros)
                {
                    if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco)))
                    {
                        pro.SetValue(Entity, null);
                    }
                }
            }
            //自动设定添加日期和添加人
            if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco)))
            {
                BasePoco ent = Entity as BasePoco;
                if (ent.CreateTime == null)
                {
                    ent.CreateTime = DateTime.Now;
                }
                if (string.IsNullOrEmpty(ent.CreateBy))
                {
                    ent.CreateBy = LoginUserInfo?.ITCode;
                }
            }

            if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(PersistPoco)))
            {
                (Entity as PersistPoco).IsValid = true;
            }

            #region 更新子表
            foreach (var pro in pros)
            {
                //找到类型为List<xxx>的字段
                if (pro.PropertyType.GenericTypeArguments.Count() > 0)
                {
                    //获取xxx的类型
                    var ftype = pro.PropertyType.GenericTypeArguments.First();
                    //如果xxx继承自TopBasePoco
                    if (ftype.IsSubclassOf(typeof(TopBasePoco)))
                    {
                        //界面传过来的子表数据
                        IEnumerable <TopBasePoco> list = pro.GetValue(Entity) as IEnumerable <BasePoco>;
                        if (list != null && list.Count() > 0)
                        {
                            string         fkname   = DC.GetFKName <TModel>(pro.Name);
                            PropertyInfo[] itemPros = ftype.GetProperties();

                            bool found = false;
                            foreach (var newitem in list)
                            {
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(newitem, null);
                                    }
                                    if (!string.IsNullOrEmpty(fkname))
                                    {
                                        if (itempro.Name.ToLower() == fkname.ToLower())
                                        {
                                            itempro.SetValue(newitem, Entity.GetID());
                                            found = true;
                                        }
                                    }
                                }
                            }
                            //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环
                            if (found == false)
                            {
                                continue;
                            }
                            //循环页面传过来的子表数据,自动设定添加日期和添加人
                            foreach (var newitem in list)
                            {
                                var      subtype = newitem.GetType();
                                BasePoco ent     = newitem as BasePoco;
                                if (ent.CreateTime == null)
                                {
                                    ent.CreateTime = DateTime.Now;
                                }
                                if (string.IsNullOrEmpty(ent.CreateBy))
                                {
                                    ent.CreateBy = LoginUserInfo?.ITCode;
                                }
                            }
                        }
                    }
                }
            }
            #endregion


            //添加数据
            DC.Set <TModel>().Add(Entity);
        }
Beispiel #3
0
        public virtual void UpdateEntityList(bool updateAllFields = false)
        {
            if (EntityList != null)
            {
                var            ftype    = EntityList.GetType().GenericTypeArguments.First();
                PropertyInfo[] itemPros = ftype.GetProperties();

                foreach (var newitem in EntityList)
                {
                    var subtype = newitem.GetType();
                    if (subtype.IsSubclassOf(typeof(BasePoco)))
                    {
                        BasePoco ent = newitem as BasePoco;
                        if (ent.UpdateTime == null)
                        {
                            ent.UpdateTime = DateTime.Now;
                        }
                        if (string.IsNullOrEmpty(ent.UpdateBy))
                        {
                            ent.UpdateBy = LoginUserInfo?.ITCode;
                        }
                    }
                    //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID
                    foreach (var itempro in itemPros)
                    {
                        if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                        {
                            itempro.SetValue(newitem, null);
                        }
                    }
                }

                IEnumerable <TopBasePoco> data = null;
                //打开新的数据库联接,获取数据库中的主表和子表数据
                using (var ndc = DC.CreateNew())
                {
                    var ids = EntityList.Select(x => x.GetID().ToString()).ToList();
                    data = ndc.Set <TModel>().AsNoTracking().Where(ids.GetContainIdExpression <TModel>()).ToList();
                }
                //比较子表原数据和新数据的区别
                IEnumerable <TopBasePoco> toadd    = null;
                IEnumerable <TopBasePoco> toremove = null;
                Utils.CheckDifference(data, EntityList, out toremove, out toadd);
                //设定子表应该更新的字段
                List <string> setnames = new List <string>();
                foreach (var field in FC.Keys)
                {
                    if (field.StartsWith("EntityList[0]."))
                    {
                        string name = field.Replace("EntityList[0].", "");
                        setnames.Add(name);
                    }
                }

                //前台传过来的数据
                foreach (var newitem in EntityList)
                {
                    //数据库中的数据
                    foreach (var item in data)
                    {
                        //需要更新的数据
                        if (newitem.GetID().ToString() == item.GetID().ToString())
                        {
                            dynamic i           = newitem;
                            var     newitemType = item.GetType();
                            foreach (var itempro in itemPros)
                            {
                                if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && (updateAllFields == true || setnames.Contains(itempro.Name)))
                                {
                                    var notmapped = itempro.GetCustomAttribute <NotMappedAttribute>();
                                    if (itempro.Name != "ID" && notmapped == null && itempro.PropertyType.IsList() == false)
                                    {
                                        DC.UpdateProperty(i, itempro.Name);
                                    }
                                }
                            }
                            if (item.GetType().IsSubclassOf(typeof(BasePoco)))
                            {
                                DC.UpdateProperty(i, "UpdateTime");
                                DC.UpdateProperty(i, "UpdateBy");
                            }
                        }
                    }
                }
                //需要删除的数据
                foreach (var item in toremove)
                {
                    //如果是PersistPoco,则把IsValid设为false,并不进行物理删除
                    if (ftype.IsSubclassOf(typeof(PersistPoco)))
                    {
                        (item as PersistPoco).IsValid    = false;
                        (item as PersistPoco).UpdateTime = DateTime.Now;
                        (item as PersistPoco).UpdateBy   = LoginUserInfo?.ITCode;
                        dynamic i = item;
                        DC.UpdateEntity(i);
                    }
                    else
                    {
                        foreach (var itempro in itemPros)
                        {
                            if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                            {
                                itempro.SetValue(item, null);
                            }
                        }
                        dynamic i = item;
                        DC.DeleteEntity(i);
                    }
                }
                //需要添加的数据
                foreach (var item in toadd)
                {
                    if (item.GetType().IsSubclassOf(typeof(BasePoco)))
                    {
                        BasePoco ent = item as BasePoco;
                        if (ent.CreateTime == null)
                        {
                            ent.CreateTime = DateTime.Now;
                        }
                        if (string.IsNullOrEmpty(ent.CreateBy))
                        {
                            ent.CreateBy = LoginUserInfo?.ITCode;
                        }
                    }
                    DC.AddEntity(item);
                }

                DC.SaveChanges();
            }
        }
Beispiel #4
0
        /// <summary>
        /// 批量修改,默认对Ids中包含的数据进行修改,子类如果有特殊判断应重载本函数
        /// </summary>
        /// <returns>true代表成功,false代表失败</returns>
        public virtual bool DoBatchEdit()
        {
            //获取批量修改VM的所有属性
            var         pros    = LinkedVM.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
            bool        rv      = true;
            List <Guid> idsData = new List <Guid>(Ids);
            //找到对应的BaseCRUDVM,并初始化
            var vmtype = this.GetType().Assembly.GetExportedTypes().Where(x => x.IsSubclassOf(typeof(BaseCRUDVM <TModel>))).FirstOrDefault();
            IBaseCRUDVM <TModel> vm = null;

            if (vmtype != null)
            {
                vm = vmtype.GetConstructor(System.Type.EmptyTypes).Invoke(null) as IBaseCRUDVM <TModel>;
                vm.CopyContext(this);
            }
            //循环所有数据
            for (int i = 0; i < idsData.Count; i++)
            {
                try
                {
                    //如果找不到对应数据,则输出错误
                    TModel entity = null;
                    entity = DC.Set <TModel>().Find(idsData[i]);
                    if (vm != null)
                    {
                        vm.SetEntity(entity);
                    }
                    if (entity == null)
                    {
                        ErrorMessage.Add(idsData[i], "数据不存在");
                        rv = false;
                        break;
                    }
                    //如果能找到,则循环LinkedVM中的属性,给entity中同名属性赋值
                    foreach (var pro in pros)
                    {
                        var proToSet = entity.GetType().GetProperty(pro.Name);
                        var val      = FC.ContainsKey("LinkedVM." + pro.Name) ? FC["LinkedVM." + pro.Name] : null;
                        if (proToSet != null && val != null)
                        {
                            var hasvalue = false;
                            if (val is StringValues sv && StringValues.IsNullOrEmpty(sv) == false)
                            {
                                hasvalue = true;
                            }
                            if (hasvalue)
                            {
                                proToSet.SetValue(entity, pro.GetValue(LinkedVM));
                            }
                        }
                    }
                    //如果有对应的BaseCRUDVM则使用其进行数据验证
                    if (vm != null)
                    {
                        vm.Validate();
                        var errors = vm.MSD;
                        if (errors != null && errors.Count > 0)
                        {
                            var error = "";
                            foreach (var key in errors.Keys)
                            {
                                if (errors[key].Count > 0)
                                {
                                    error += errors[key].Select(x => x.ErrorMessage).ToSpratedString();
                                }
                            }
                            if (error != "")
                            {
                                ErrorMessage.Add(idsData[i], error);
                                rv = false;
                                break;
                            }
                        }
                    }
                    if (typeof(TModel).IsSubclassOf(typeof(BasePoco)))
                    {
                        BasePoco ent = entity as BasePoco;
                        if (ent.CreateTime == null)
                        {
                            ent.CreateTime = DateTime.Now;
                        }
                        if (string.IsNullOrEmpty(ent.CreateBy))
                        {
                            ent.CreateBy = LoginUserInfo?.ITCode;
                        }
                    }
                    DC.UpdateEntity(entity);
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, idsData[i]);
                    rv = false;
                }
            }
            //进行数据库的修改操作
            if (rv == true)
            {
                try
                {
                    DC.SaveChanges();
                }
                catch (Exception e)
                {
                    SetExceptionMessage(e, null);
                    rv = false;
                }
            }

            //如果有错误,输出错误信息
            if (rv == false)
            {
                if (ErrorMessage.Count > 0)
                {
                    foreach (var id in idsData)
                    {
                        if (!ErrorMessage.ContainsKey(id))
                        {
                            ErrorMessage.Add(id, "已回滚");
                        }
                    }
                }
                ListVM.DoSearch();
                foreach (var item in ListVM.GetEntityList())
                {
                    item.BatchError = ErrorMessage.Where(x => x.Key == item.ID).Select(x => x.Value).FirstOrDefault();
                }
            }
            return(rv);
        }
        /// <summary>
        /// 修改,进行默认的修改操作。子类如有自定义操作应重载本函数
        /// </summary>
        public virtual void DoEdit(bool updateAllFields = false)
        {
            //自动设定修改日期和修改人
            if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco)))
            {
                BasePoco ent = Entity as BasePoco;
                if (ent.UpdateTime == null)
                {
                    ent.UpdateTime = DateTime.Now;
                }
                if (string.IsNullOrEmpty(ent.UpdateBy))
                {
                    ent.UpdateBy = LoginUserInfo?.ITCode;
                }
            }
            var pros = typeof(TModel).GetProperties();

            #region 更新子表
            foreach (var pro in pros)
            {
                //找到类型为List<xxx>的字段
                if (pro.PropertyType.GenericTypeArguments.Count() > 0)
                {
                    //获取xxx的类型
                    var ftype = pro.PropertyType.GenericTypeArguments.First();
                    //如果xxx继承自TopBasePoco
                    if (ftype.IsSubclassOf(typeof(TopBasePoco)))
                    {
                        //界面传过来的子表数据

                        if (pro.GetValue(Entity) is IEnumerable <TopBasePoco> list && list.Count() > 0)
                        {
                            //获取外键字段名称
                            string         fkname   = DC.GetFKName <TModel>(pro.Name);
                            PropertyInfo[] itemPros = ftype.GetProperties();

                            bool found = false;
                            foreach (var newitem in list)
                            {
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(newitem, null);
                                    }
                                    if (!string.IsNullOrEmpty(fkname))
                                    {
                                        if (itempro.Name.ToLower() == fkname.ToLower())
                                        {
                                            itempro.SetValue(newitem, Entity.ID);
                                            found = true;
                                        }
                                    }
                                }
                            }
                            //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环
                            if (found == false)
                            {
                                continue;
                            }


                            //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID
                            foreach (var newitem in list)
                            {
                                var subtype = newitem.GetType();
                                if (subtype.IsSubclassOf(typeof(BasePoco)))
                                {
                                    BasePoco ent = newitem as BasePoco;
                                    if (ent.UpdateTime == null)
                                    {
                                        ent.UpdateTime = DateTime.Now;
                                    }
                                    if (string.IsNullOrEmpty(ent.UpdateBy))
                                    {
                                        ent.UpdateBy = LoginUserInfo.ITCode;
                                    }
                                }
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(newitem, null);
                                    }
                                    if (!string.IsNullOrEmpty(fkname))
                                    {
                                        if (itempro.Name.ToLower() == fkname.ToLower())
                                        {
                                            itempro.SetValue(newitem, Entity.ID);
                                            found = true;
                                        }
                                    }
                                }
                            }
                            TModel _entity = null;
                            //打开新的数据库联接,获取数据库中的主表和子表数据
                            using (var ndc = DC.CreateNew())
                            {
                                _entity = ndc.Set <TModel>().Include(pro.Name).AsNoTracking().Where(x => x.ID == Entity.ID).FirstOrDefault();
                            }
                            //比较子表原数据和新数据的区别
                            IEnumerable <TopBasePoco> toadd    = null;
                            IEnumerable <TopBasePoco> toremove = null;
                            IEnumerable <TopBasePoco> data     = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>;
                            Utils.CheckDifference(data, list, out toremove, out toadd);
                            //设定子表应该更新的字段
                            List <string> setnames = new List <string>();
                            foreach (var field in FC.Keys)
                            {
                                if (field.StartsWith("Entity." + pro.Name + "[0]."))
                                {
                                    string name = field.Replace("Entity." + pro.Name + "[0].", "");
                                    if (name != "UpdateTime" && name != "UpdateBy")
                                    {
                                        setnames.Add(name);
                                    }
                                }
                            }

                            //前台传过来的数据
                            foreach (var newitem in list)
                            {
                                //数据库中的数据
                                foreach (var item in data)
                                {
                                    //需要更新的数据
                                    if (newitem.ID == item.ID)
                                    {
                                        dynamic i           = newitem;
                                        var     newitemType = item.GetType();
                                        foreach (var itempro in itemPros)
                                        {
                                            if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && setnames.Contains(itempro.Name))
                                            {
                                                if (itempro.Name != "ID")
                                                {
                                                    DC.UpdateProperty(i, itempro.Name);
                                                }
                                            }
                                        }

                                        DC.UpdateProperty(i, "UpdateTime");
                                        DC.UpdateProperty(i, "UpdateBy");
                                    }
                                }
                            }
                            //需要删除的数据
                            foreach (var item in toremove)
                            {
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(item, null);
                                    }
                                }
                                dynamic i = item;
                                DC.DeleteEntity(i);
                            }
                            //需要添加的数据
                            foreach (var item in toadd)
                            {
                                if (item.GetType().IsSubclassOf(typeof(BasePoco)))
                                {
                                    BasePoco ent = item as BasePoco;
                                    if (ent.CreateTime == null)
                                    {
                                        ent.CreateTime = DateTime.Now;
                                    }
                                    if (string.IsNullOrEmpty(ent.CreateBy))
                                    {
                                        ent.CreateBy = LoginUserInfo?.ITCode;
                                    }
                                }
                                DC.AddEntity(item);
                            }
                        }
                        else if (FC.Keys.Contains("Entity." + pro.Name + ".DONOTUSECLEAR"))
                        {
                            PropertyInfo[]            itemPros   = ftype.GetProperties();
                            var                       _entity    = DC.Set <TModel>().Include(pro.Name).AsNoTracking().Where(x => x.ID == Entity.ID).FirstOrDefault();
                            IEnumerable <TopBasePoco> removeData = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>;
                            foreach (var item in removeData)
                            {
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(item, null);
                                    }
                                }
                                dynamic i = item;
                                DC.DeleteEntity(i);
                            }
                        }
                    }
                }
            }
            #endregion


            if (updateAllFields == false)
            {
                foreach (var field in FC.Keys)
                {
                    if (field.StartsWith("Entity.") && !field.Contains("["))
                    {
                        string name = field.Replace("Entity.", "");
                        if (name != "UpdateTime" && name != "UpdateBy")
                        {
                            try
                            {
                                DC.UpdateProperty(Entity, name);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
            else
            {
                DC.UpdateEntity(Entity);
            }

            DC.SaveChanges();
            //删除不需要的附件
            if (DeletedFileIds != null)
            {
                foreach (var item in DeletedFileIds)
                {
                    FileAttachmentVM ofa = new FileAttachmentVM();
                    ofa.CopyContext(this);
                    ofa.SetEntityById(item);
                    ofa.DoDelete();
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// 添加,进行默认的添加操作。子类如有自定义操作应重载本函数
        /// </summary>
        public virtual void DoAdd()
        {
            var pros = typeof(TModel).GetProperties();

            //将所有TopBasePoco的属性赋空值,防止添加关联的重复内容
            if (typeof(TModel) != typeof(FileAttachment))
            {
                foreach (var pro in pros)
                {
                    if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco)))
                    {
                        pro.SetValue(Entity, null);
                    }
                }
            }
            //自动设定添加日期和添加人
            if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco)))
            {
                BasePoco ent = Entity as BasePoco;
                if (ent.CreateTime == null)
                {
                    ent.CreateTime = DateTime.Now;
                }
                if (string.IsNullOrEmpty(ent.CreateBy))
                {
                    ent.CreateBy = LoginUserInfo?.ITCode;
                }
            }

            if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(PersistPoco)))
            {
                (Entity as PersistPoco).IsValid = true;
            }

            #region 更新子表
            foreach (var pro in pros)
            {
                //找到类型为List<xxx>的字段
                if (pro.PropertyType.GenericTypeArguments.Count() > 0)
                {
                    //获取xxx的类型
                    var ftype = pro.PropertyType.GenericTypeArguments.First();
                    //如果xxx继承自TopBasePoco
                    if (ftype.IsSubclassOf(typeof(BasePoco)))
                    {
                        //界面传过来的子表数据
                        IEnumerable <TopBasePoco> list = pro.GetValue(Entity) as IEnumerable <BasePoco>;
                        if (list != null && list.Count() > 0)
                        {
                            string         fkname   = DC.GetFKName <TModel>(pro.Name);
                            PropertyInfo[] itemPros = ftype.GetProperties();

                            bool found = false;
                            foreach (var newitem in list)
                            {
                                foreach (var itempro in itemPros)
                                {
                                    if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)))
                                    {
                                        itempro.SetValue(newitem, null);
                                    }
                                    if (!string.IsNullOrEmpty(fkname))
                                    {
                                        if (itempro.Name.ToLower() == fkname.ToLower())
                                        {
                                            itempro.SetValue(newitem, Entity.ID);
                                            found = true;
                                        }
                                    }
                                }
                            }
                            //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环
                            if (found == false)
                            {
                                continue;
                            }
                            //循环页面传过来的子表数据,自动设定添加日期和添加人
                            foreach (var newitem in list)
                            {
                                var      subtype = newitem.GetType();
                                BasePoco ent     = newitem as BasePoco;
                                if (ent.CreateTime == null)
                                {
                                    ent.CreateTime = DateTime.Now;
                                }
                                if (string.IsNullOrEmpty(ent.CreateBy))
                                {
                                    ent.CreateBy = LoginUserInfo?.ITCode;
                                }

                                var ft = subtype.GetProperties().Where(x => x.PropertyType == typeof(FileAttachment)).ToList();
                                foreach (var f in ft)
                                {
                                    var fileid = subtype.GetProperty(f.Name + "ID").GetValue(ent);
                                    if (fileid != null)
                                    {
                                        var file = DC.Set <FileAttachment>().Find(fileid);
                                        file.IsTemprory = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion


            //添加数据
            DC.Set <TModel>().Add(Entity);
            //如果数据中包括附件,则把附件的是否临时的属性设为false,代表已经有数据用这个附件了
            var fa = typeof(TModel).GetProperties().Where(x => x.PropertyType == typeof(FileAttachment)).ToList();
            foreach (var f in fa)
            {
                var fileid = typeof(TModel).GetProperty(f.Name + "Id").GetValue(Entity);
                if (fileid != null)
                {
                    var file = DC.Set <FileAttachment>().Find(fileid);
                    file.IsTemprory = false;
                }
            }

            //删除不需要的附件
            if (DeletedFileIds != null)
            {
                foreach (var item in DeletedFileIds)
                {
                    FileAttachmentVM ofa = new FileAttachmentVM();
                    ofa.CopyContext(this);
                    ofa.DoDelete();
                }
            }
            DC.SaveChanges();
        }