[英]How can I dynamically connect to different databases with Entity Framework 6?
I'm working with WinForms and .NET 4.0, using EF 6 with code first. 我正在使用WinForms和.NET 4.0,首先使用EF 6和代码。
I need to offer the possibility to connect to a MySQL or a SQL Server DB. 我需要提供连接到MySQL或SQL Server DB的可能性。 For the moment I use different connection strings in the XML app.config
file and I change the name in the DBContext
class. 目前,我在XML app.config
文件中使用了不同的连接字符串,并在DBContext
类中更改了名称。 How can I make that work dynamically instead? 我怎样才能使它动态地工作呢?
//first DbContext
namespace MultiDataContextMigrations.Models
{
public class DataContext : DbContext
{
public DataContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//TODO:Define mapping
}
public DbSet Users { get; set; }
public DbSet Orders { get; set; }
}
}
//second DbContext
namespace MultiDataContextMigrations.Models
{
public class UserDataContext : DbContext
{
public UserDataContext():base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//TODO:Define mapping
}
public DbSet Users { get; set; }
public DbSet Roles { get; set; }
}
}
Check this Link http://www.dotnet-tricks.com/Tutorial/entityframework/2VOa140214-Entity-Framework-6-Code-First-Migrations-with-Multiple-Data-Contexts.html 检查此链接http://www.dotnet-tricks.com/Tutorial/entityframework/2VOa140214-Entity-Framework-6-Code-First-Migrations-with-Multiple-Data-Contexts.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.