TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sneha K
1.2k
527
195.8k
Change database in db.Context file dynamically in run time
May 1 2018 10:27 AM
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<Address> Addresses {
get
;
set
; }
public
virtual
DbSet<AddressType> AddressTypes {
get
;
set
; }
public
virtual
DbSet<Area> Areas {
get
;
set
; }
public
virtual
DbSet<Bank> Banks {
get
;
set
; }
public
virtual
DbSet<City> Cities {
get
;
set
; }
public
virtual
DbSet<ContactType> 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
Reply
Answers (
2
)
Can we use / instead of ? for passing querystring value
Entity framework code first migration issue