Пример #1
0
        //public static UserModel ToUserModel(this User user, IDataBaseProvider provider)
        //{
        //    if (provider == null)
        //        throw new ArgumentNullException(nameof(provider));
        //    UserModel model = new UserModel()
        //    {
        //        Id = user.ID_User,
        //        EMail = user.E_mail,
        //        Login = user.Login,
        //        Name = user.Name
        //    };
        //    return model;
        //}

        public static ClientType ToClientType(this Client_Type type)
        {
            ClientType tp = new ClientType()
            {
                ID       = type.ID_Client_Type,
                TypeName = type.Type_Name
            };

            return(tp);
        }
Пример #2
0
        public decimal CreateAClientType(string typeName, decimal id)
        {
            if (string.IsNullOrWhiteSpace(typeName))
            {
                throw new ArgumentNullException(nameof(typeName));
            }
            Client_Type newType = new Client_Type()
            {
                Type_Name      = typeName,
                ID_Client_Type = id
            };

            using (var context = _ContextGenerator.GenerateContext())
            {
                try
                {
                    context.Client_Type.Add(newType);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    context.Client_Type.Remove(newType);
                    List <ValidationError> validationErrors = new List <ValidationError>();
                    foreach (var validResult in ex.EntityValidationErrors)
                    {
                        validationErrors.AddRange(validResult.ValidationErrors.Select(
                                                      v => new ValidationError(v.PropertyName, v.ErrorMessage)));
                    }
                    throw new InsertDbDataException(validationErrors);
                }
                catch (DbUpdateException ex)
                {
                    context.Client_Type.Remove(newType);
                    throw new InsertDbDataException($"Update entity error: {ex.Message}");
                }
            }
            return(newType.ID_Client_Type);
        }