Пример #1
0
        public async Task <bool> AddOrUpdateAsync(Sys_User user)
        {
            try
            {
                var sysUser = await context.Sys_Users.Where(p => p.Uid == user.Uid).FirstOrDefaultAsync();

                bool isNew = false;

                if (sysUser == null)
                {
                    isNew   = true;
                    sysUser = new Sys_User();
                }
                foreach (var p in sysUser.GetType().GetProperties())
                {
                    //更新属性
                    var v = user.GetType().GetProperty(p.Name).GetValue(user);
                    if (v != null)
                    {
                        //其他字段更新
                        p.SetValue(sysUser, v);
                    }
                }
                if (isNew)
                {
                    context.Sys_Users.Add(sysUser);
                }
                return(await context.SaveChangesAsync() == 1);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }