Hi @Anjali Agarwal
How can I define the connection string for database2 in startup.cs file or access the database2 in the controller in order to insert the data from one database to another one
It is similar with the database1. To use multiple Database and DbContext in the asp.net core application. In the appsettings.json file, you can add two connection strings (change the connection string to yours):
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=database1;Trusted_Connection=True;MultipleActiveResultSets=true",
"SecondDbConnection": "Server=(localdb)\\mssqllocaldb;Database=database2;Trusted_Connection=True;MultipleActiveResultSets=true"
},
Then, in the startup.cs/program.cs file, you can configure the DbContext like this (change the ApplicationDbContext and SecondDBContext to your DbContext):
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<SecondDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SecondDbConection")));
The DbContext as below:
public class SecondDBContext:DbContext
{
public DbSet<Employee> Employees { get; set; }
public SecondDBContext(DbContextOptions<SecondDBContext> options)
: base(options)
{
}
}
Then, in the controller, you can inject the DbContext as below:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IPasswordHasher<ApplicationUser> _passwordHasher;
private readonly ApplicationDbContext _dbContext; //first dbcontext
private readonly SecondDBContext _secondDbContext; //second dbcontext
public HomeController(ILogger<HomeController> logger,
ApplicationDbContext applicationDbContext,
SecondDBContext secondDBContext)
{
_logger = logger;
_dbContext=applicationDbContext;
_secondDbContext=secondDBContext;
_passwordHasher=passwordHasher;
}
public IActionResult Index()
{
//query data from the first DbContext,
//since the primary key is auto insert, when insert the data into the new database, there is no need to set its value.
var employees = (_dbContext.Employees.Select(v => new Employee()
{
Name = v.Name,
Address = v.Address,
Date = v.Date,
Surname = v.Surname,
Gender = v.Gender
})).ToList();
//insert the data into the second Database.
_secondDbContext.Employees.AddRange(employees);
_secondDbContext.SaveChanges();
var vm = new EmployeeViewModel();
return View();
}
Note: Since there has multiple DbContext, when enable migration, you have to assign the DbContext using the -context
option. For example:
add-migration addseconddb -context SecondDBContext
update-database -context SecondDBContext
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion