/// <summary> /// HiepPD1 /// Add new config connection /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void AddConnectionConfig(string key, string value) { TblConnectionConfig tblConnectionConfig = new TblConnectionConfig(); tblConnectionConfig.ConnectionKey = key; tblConnectionConfig.ConnectionValue = value; db.TblConnectionConfig.Add(tblConnectionConfig); db.SaveChanges(); }
/// <summary> /// Add context when create department /// </summary> public void AddChildConnectionString(TblConnectionConfig connectionStrings) { var optionsBuilder = new DbContextOptionsBuilder <CRM_MPContext>(); optionsBuilder.UseSqlServer(connectionStrings.ConnectionValue); CRM_MPContext dbcontext = new CRM_MPContext(optionsBuilder.Options); DbContextMap.Add(connectionStrings.ConnectionKey, dbcontext); }
/// <summary> /// Add context when create department /// </summary> public static DbContextFactory AddChildContext(TblConnectionConfig connectionStrings) { if (dbContextFactory == null) { dbContextFactory = new DbContextFactory(); } dbContextFactory.AddChildConnectionString(connectionStrings); return(dbContextFactory); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseAuthentication(); app.UseHttpsRedirection(); app.UseMvc(); //Multiple database Dictionary <string, string> connStrs = new Dictionary <string, string>(); #region Old: Get from appsetting //var lstConnection = Configuration.GetSection(AttributeConstant.ConnectionConfig).AsEnumerable().ToList(); //if (lstConnection != null) //{ // for (int i = 1; i < lstConnection.Count; i++) // { // connStrs.Add(lstConnection[i].Key.Replace(AttributeConstant.ConnectionConfigReplace, string.Empty), lstConnection[i].Value); // } //} #endregion VOCCommon attributeCommon = new VOCCommon(); List <TblConnectionConfig> lstConn = new List <TblConnectionConfig>(); var objConnection = attributeCommon.GetAllConnection(); if (objConnection.Count > 0) { for (int i = 0; i < objConnection[0].Count; i++) { TblConnectionConfig item = objConnection[0][i] as TblConnectionConfig; connStrs.Add(item.ConnectionKey, item.ConnectionValue); lstConn.Add(item); } } VOCCommon.ListConnectionStrings = lstConn; DbContextFactory dbContextFactory = DbContextFactory.getInstance(connStrs); //end app.UseHttpsRedirection(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Category}/{action=GetAllCategory}/{id?}"); }); }
/// <summary> /// Delete Organization /// </summary> /// <param name="OrganizationId"></param> /// <returns></returns> public int DeleteOrganization(int OrganizationId) { int result = 0; try { var lstOrgCheck = (from a in db.TblOrganizationUser join b in db.TblUsers on a.UserId equals b.Id where b.IsDelete == false && a.OrganizationId == OrganizationId select new { b.Id }); if (lstOrgCheck.Count() > 1) { result = 0; } else { using (var ts = new TransactionScope()) { TblOrganization OrgChk = db.TblOrganization.Where(a => a.OrganizationId == OrganizationId).FirstOrDefault(); if (OrgChk != null) { OrgChk.IsDelete = true; db.Entry(OrgChk).State = EntityState.Modified; db.SaveChanges(); TblConnectionConfig connect = db.TblConnectionConfig.Where(a => a.ConnectionKey == OrgChk.OrganizationCode + "Connection").FirstOrDefault(); if (connect != null) { db.TblConnectionConfig.Remove(connect); db.SaveChanges(); } string urlImage = _environment.WebRootPath + OrganizationConstant.DirectoryUploads + OrgChk.OrganizationCode + "\\" + OrgChk.OrganizationCode + ".png"; try { File.Delete(urlImage); } catch (Exception ex) { Console.WriteLine(ex.Message); } DeleteUser(OrganizationId); result = 1; } ts.Complete(); } } return(result); } catch (Exception ex) { return(result); throw ex; } }
public void SetContextFactory(TblConnectionConfig connectionStrings) { DbContextFactory.AddChildContext(connectionStrings); }