gpt4 book ai didi

c# - 在 EntityFramework 中为同一个 DBContext 使用多个连接字符串

转载 作者:太空狗 更新时间:2023-10-30 00:49:08 25 4
gpt4 key购买 nike

我正在使用 EntityFramework 开发一个项目,我需要从与默认数据库不同的数据库中获取部分模型数据。如何使用具有相同 DbContext 的多个连接字符串?

public class HMVoltageDBContext : DbContext, IDisposable// IdentityDbContext<User>, IDisposable
{
static HMVoltageDBContext()
{
Database.SetInitializer<HMVoltageDBContext>(new HMVoltageDatabaseInitializer());
}

public HMVoltageDBContext() : base(nameOrConnectionString: "HMVoltageDB") {
Maps = base.Set<Map>();
MapSteps = base.Set<MapStep>();
MapParents = base.Set<MapParents>();
MapTypes = base.Set<MapType>();
PageSizes = base.Set<PageSize>();
PreparationPlaces = base.Set<PreparationPlace>();
Products = base.Set<Product>();
ProductCategories = base.Set<ProductCategory>();
Statuses = base.Set<Status>();
Steps = base.Set<Step>();
Sets = base.Set<Set>();
MapReceivers = base.Set<MapReceiver>();
Receivers = base.Set<Receiver>();

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Use singular table names
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new MapConfiguration());
modelBuilder.Configurations.Add(new MapParentsConfiguration());
modelBuilder.Configurations.Add(new MapStepConfiguration());
modelBuilder.Configurations.Add(new MapTypeConfiguration());
modelBuilder.Configurations.Add(new PageSizeConfiguration());
modelBuilder.Configurations.Add(new PreparationPlaceConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());
modelBuilder.Configurations.Add(new ProductCategoryConfiguration());
modelBuilder.Configurations.Add(new StatusConfiguration());
modelBuilder.Configurations.Add(new StepConfiguration());
modelBuilder.Configurations.Add(new SetConfiguration());
modelBuilder.Configurations.Add(new MapReceiverConfiguration());
modelBuilder.Configurations.Add(new ReceiverConfiguration());

}
public DbSet<Map> Maps { get; set; }
public DbSet<MapParents> MapParents { get; set; }
public DbSet<MapReceiver> MapReceivers { get; set; }
public DbSet<MapStep> MapSteps { get; set; }
public DbSet<MapType> MapTypes { get; set; }
public DbSet<PageSize> PageSizes { get; set; }
public DbSet<PreparationPlace> PreparationPlaces { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductCategory> ProductCategories { get; set; }
public DbSet<Receiver> Receivers { get; set; }
public DbSet<Status> Statuses { get; set; }
public DbSet<Set> Sets { get; set; }
public DbSet<Step> Steps { get; set; }
public static HMVoltageDBContext Create()
{
return new HMVoltageDBContext();
}
public static void init()
{
try
{
Create().Database.Initialize(true);
}
catch (Exception ex)
{
throw ex;
}
}
}

这是我的连接字符串:

<connectionStrings>
<add name="HMVoltageDB" connectionString="Data Source=.; Database = HMV; User Id=sa; Password=123456; " providerName="System.Data.SqlClient"/>
</connectionStrings>

最佳答案

您可以使用连接字符串的名称重载 DbContext 的构造函数,如下所示。

public class HMVoltageDBContext : DbContext
{
public HMVoltageDBContext(string nameOrConnectionString) : base(nameOrConnectionString)
{

}
}

之后,您可以在实例化 DbContext 时传递配置的连接字符串或连接字符串的名称。

var context = new HMVoltageDBContext( "you-can-change-conn-at-runtime-here");

关于c# - 在 EntityFramework 中为同一个 DBContext 使用多个连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40354460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com