public string AddBusinessConfiguration(Core.DomainModel.Business Business)
        {
            if (!BusinessRule.CheckConfigurationCount(Business))
            {
                throw new ConfigurationCountLimitExceededException("1 configuration set should not have more than 3 configurations!");
            }

            if (!BusinessRule.CheckConfigurationType(Business))
            {
                throw new DuplicatedConfigurationTypeException("1 configuration set should not have 2 or more configurations that are of the same type!");
            }

            Dictionary <string, string> suspectConfs = null;

            if (!BusinessRule.CheckConfigurationDBConnectionString(Business, out suspectConfs))
            {
                throw new DuplicatedDatabaseException("1 configuration set should not have 2 or more configurations that are using the same database!");
            }
            else if ((suspectConfs != null) && (suspectConfs.Count > 0))
            {
                string message = "Suspect databases found in the bindings of the following configurations, please double check to make sure that they are not the same database:";

                string suspectConfListTemplate = "Business Name: \"{0}\", Server Name: \"{1}\"; ";

                message += System.Environment.NewLine;

                foreach (string bizName in suspectConfs.Keys)
                {
                    message += string.Format(suspectConfListTemplate, bizName, suspectConfs[bizName]);
                    message += System.Environment.NewLine;
                }

                throw new DuplicatedDatabaseException(message);
            }

            if (!BusinessRule.ValidateConfigurationDBConnectionString(Business))
            {
                throw new InvalidConnectionStringException("Connection string is invalid! Make sure each field of the connection string has its value assigned, and also make sure loopback address is not used.");
            }

            DateTime creationTime = DateTime.Now;

            using (DataModelContainer container = new DataModelContainer())
            {
                Business biz = new Business()
                {
                    Id               = Business.ID,
                    Name             = Business.Name,
                    CreationTime     = creationTime,
                    ModificationTime = creationTime
                };

                //if (String.IsNullOrEmpty(Customer.ReferenceID))
                //{
                //    cust.ReferenceId = Customer.ReferenceID;
                //}

                if (Business.ReferenceID != null)
                {
                    biz.ReferenceId = "";

                    for (int i = 0; i < Business.ReferenceID.Length; i++)
                    {
                        biz.ReferenceId += Business.ReferenceID[i];

                        if (i != (Business.ReferenceID.Length - 1))
                        {
                            biz.ReferenceId += ",";
                        }
                    }
                }

                container.Businesses.Add(biz);

                foreach (var conf in Business.Configurations)
                {
                    if (conf != null)
                    {
                        Configuration configuration = new Configuration()
                        {
                            Business           = biz,
                            Id                 = conf.ID,
                            BusinessId         = biz.Id,
                            DbConnectionString = conf.DbConnectionString,
                            TypeId             = (int)(conf.ConfigurationType),
                            CreationTime       = creationTime,
                            ModificationTime   = creationTime
                        };

                        container.Configurations.Add(configuration);
                    }
                }

                container.SaveChanges();
            }

            return(Business.ID);
        }
        public int UpdateBusinessConfiguration(Core.DomainModel.Business Business)
        {
            if (!BusinessRule.CheckConfigurationCount(Business))
            {
                throw new ConfigurationCountLimitExceededException("1 configuration set should not have more than 3 configurations!");
            }

            if (!BusinessRule.CheckConfigurationType(Business))
            {
                throw new DuplicatedConfigurationTypeException("1 configuration set should not have 2 or more configurations that are of the same type!");
            }

            Dictionary <string, string> suspectConfs = null;

            if (!BusinessRule.CheckConfigurationDBConnectionString(Business, out suspectConfs))
            {
                throw new DuplicatedDatabaseException("1 configuration set should not have 2 or more configurations that are using the same database!");
            }
            else if ((suspectConfs != null) && (suspectConfs.Count > 0))
            {
                string message = "Suspect databases found in the bindings of the following configurations, please double check to make sure that they are not the same database:";

                string suspectConfListTemplate = "Business Name: \"{0}\", Server Name: \"{1}\"; ";

                message += System.Environment.NewLine;

                foreach (string bizName in suspectConfs.Keys)
                {
                    message += string.Format(suspectConfListTemplate, bizName, suspectConfs[bizName]);
                    message += System.Environment.NewLine;
                }

                throw new DuplicatedDatabaseException(message);
            }

            if (!BusinessRule.ValidateConfigurationDBConnectionString(Business))
            {
                throw new InvalidConnectionStringException("Connection string is invalid! Make sure each field of the connection string has its value assigned, and also make sure loopback address is not used.");
            }

            int returnValue = -9;

            DateTime modificationTime = DateTime.Now;

            using (DataModelContainer container = new DataModelContainer())
            {
                Business biz = container.Businesses.First((o) => (o.Id.ToLower() == Business.ID.ToLower()));

                biz.Name             = Business.Name;
                biz.ModificationTime = modificationTime;

                //cust.ReferenceId = Customer.ReferenceID;

                if (Business.ReferenceID != null)
                {
                    biz.ReferenceId = "";

                    for (int i = 0; i < Business.ReferenceID.Length; i++)
                    {
                        biz.ReferenceId += Business.ReferenceID[i];

                        if (i != (Business.ReferenceID.Length - 1))
                        {
                            biz.ReferenceId += ",";
                        }
                    }
                }

                var confs = container.Configurations.Where((o) => (o.BusinessId == Business.ID)).ToArray();

                List <Core.DomainModel.Configuration> bizConfList = new List <Core.DomainModel.Configuration>(Business.Configurations);

                for (int i = 0; i < confs.Length; i++)
                {
                    for (int j = 0; j < bizConfList.Count; j++)
                    {
                        if ((confs[i] != null) && (bizConfList[j] != null))
                        {
                            if (confs[i].Id.ToLower() == bizConfList[j].ID.ToLower())
                            {
                                confs[i].DbConnectionString = bizConfList[j].DbConnectionString;
                                confs[i].TypeId             = ((int)(bizConfList[j].ConfigurationType));
                                confs[i].ModificationTime   = modificationTime;

                                bizConfList.RemoveAt(j);
                                j--;
                            }
                        }
                    }
                }

                if (bizConfList.Count > 0)
                {
                    foreach (var conf in bizConfList)
                    {
                        if (conf != null)
                        {
                            Configuration configuration = new Configuration()
                            {
                                Business           = biz,
                                Id                 = conf.ID,
                                BusinessId         = biz.Id,
                                DbConnectionString = conf.DbConnectionString,
                                TypeId             = (int)(conf.ConfigurationType),
                                CreationTime       = modificationTime,
                                ModificationTime   = modificationTime
                            };

                            container.Configurations.Add(configuration);
                        }
                    }
                }

                returnValue = container.SaveChanges();
            }

            return(returnValue);
        }