public void UpdateKeyTypeConfiguration(KeyTypeConfiguration config, bool shouldUpdateKeys)
        {
            using (var context = GetContext())
            {
                KeyTypeConfiguration configInDb = context.KeyTypeConfigurations.Single(c => c.KeyTypeConfigurationId == config.KeyTypeConfigurationId);
                context.Entry(configInDb).CurrentValues.SetValues(config);

                if (shouldUpdateKeys)
                {
                    string hqIdString = GetSearchCriteria(config.HeadQuarterId);
                    string sqlString = string.Format(@"UPDATE KeyInfoEx
                                            SET KeyType= {0}
                                            FROM KeyInfoEx ex JOIN ProductKeyInfo info ON info.ProductKeyID = ex.ProductKeyID
                                            WHERE info.LicensablePartNumber = '{1}' AND ex.HQID {2}",
                                             (int)configInDb.KeyType.Value, config.LicensablePartNumber, hqIdString);
                    context.Database.ExecuteSqlCommand(sqlString);
                }
                context.SaveChanges();
            }
        }
 public void UpdateKeyTypeConfiguration(KeyTypeConfiguration keyTypeConfiguration, bool shouldUpdateKeys)
 {
     if (keyTypeConfiguration == null)
         throw new ArgumentNullException("params error: keyTypeConfiguration is null");
     repository.UpdateKeyTypeConfiguration(keyTypeConfiguration, shouldUpdateKeys);
 }
        private void MappingKeyTypeConfiguration(KeyStoreContext context, int? hqId)
        {
            string hqIdString = GetSearchCriteria(hqId);
            string sql = string.Format(@"SELECT INFO.LicensablePartNumber, EX.KeyType
                                FROM ProductKeyInfo INFO JOIN KeyInfoEx EX ON INFO.ProductKeyID = EX.ProductKeyID
                                WHERE EX.HQID {0}
                                AND NOT EXISTS ( SELECT 1 FROM KeyTypeConfiguration WHERE HeadQuarterId {0} AND LicensablePartNumber = info.LicensablePartNumber)
                                GROUP BY INFO.LicensablePartNumber, EX.KeyType", hqIdString);

            var maps = Select<LicensablePartNumberAndKeyTypeMap>(context, sql).ToList();

            if (maps.Any())
            {
                foreach (var map in maps)
                {
                    KeyTypeConfiguration config = new KeyTypeConfiguration
                    {
                        HeadQuarterId = hqId,
                        LicensablePartNumber = map.LicensablePartNumber,
                        Maximum = MaxKeyCounts,
                        Minimum = MinKeyCounts,
                        KeyType = (KeyType?)map.KeyType
                    };
                    context.KeyTypeConfigurations.Add(config);
                }

                context.SaveChanges();
            }
        }