Hi i want to change database name dynamically in db.context file in asp.net mvc.I am passing database name dynamically in db.context file. It is working but once i refresh the edmx file means my dynamic coding is automatically cleared in db.context file. i want to change database dynamically in run time in asp.net mvc. Here i tried in db.context file its working but once i update or refresh the edmx means it clear my code.
My Connection string code in web.config file
- <connectionStrings>
- <add name="CRMEntities" connectionString="Data Source=192.168.0.73\SQLEXPRESS,14330;Initial Catalog=CRM;User ID=sa;Password=wafewin;
- MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
- connectionStrings>
My Coding in db.context file
MyCRM
- namespace CostToWafe
- {
- using System;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- public partial class CRMEntities : DbContext
- {
- public CRMEntities() : base()
- {
- this.Database.Connection.ConnectionString = "Presist Security Info=False;User ID =sa;Password=**"+GLOBAL.ZDATABASE+"**;Intial Catalog=SS1718;Data Soure=192.168.70.41";
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- throw new UnintentionalCodeFirstException();
- }
- public virtual DbSet Addresses { get; set; }
- public virtual DbSet
AddressTypes { get; set; } - public virtual DbSet Areas { get; set; }
- public virtual DbSet
Banks { get; set; } - public virtual DbSet
Cities { get; set; } - public virtual DbSet
ContactTypes { get; set; } - }
- }
In this GLOBAL.ZDATABASE i pass the database name dynamically in run time. Its working but one i update or fresh edmx file means this code is cleared automatically.
My Controller code
Visitors form Controller
- public class VisitorsFormController : Controller
- {
- CRMEntities db = new CRMEntities();
- // GET: /VisitorsForm/
- public ActionResult Index()
- {
- return View()
- }
- }
Now my doubt is it possible to connect multiple database dynamically in db.context file or any other place without change in web.config file in run time
Anyone please help me to resolve this issue to change database name dynamically in db.context file and connect in controller and also please tell another method to change database dynamically in runtime mvc .
Thanks all
Jishan SiddiquePosted Dec 22, 2020, 4:39 PM
https://stackoverflow.com/a/18841661/5855884
Vignesh ManiPosted May 1, 2018, 11:20 AM