有一个项目,我有我的应用程序,它有它的数据库和模型,但我需要连接到另一个数据库,我需要从中导入。我发现很多文章都展示了如何设置第二个数据库,但目前尚不清楚如何在创建模型时使用它。我会说我现在一直在研究这个问题,似乎无法找到一个连贯的答案。
以下是一些要点
主要问题 是否有适当的工作流程,当第二个数据库只读时,允许从第二个数据库导入应用程序的数据库?
这是我到目前为止所拥有的
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<FDWDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("FDWConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 24;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
}
DBContext.cs
public class FDWDbContext : DbContext
{
public FDWDbContext(DbContextOptions<FDWDbContext> options)
: base(options)
{
// the second database
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
// the app's database
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
public DbSet<ApplicationUser> user { get; set; }
public DbSet<invoice> invoice{ get; set; }
public DbSet<expense> expense { get; set; }
}
即时创建 这里的麻烦是我没有找到关于这个主题的太多信息。所以我的想法是建立一个参考表并使用这样的东西来映射和缓存(DB通常不会快速改变),然后“创建”这样的模型。 How to get Column name and corresponding Database Type from DbContext in Entity Framework Core是我从中得到的想法。
似乎过度杀戮。
逆向工程 另一种想法就是把它吸收起来,从字面上看并为此设置模型。
Create Entity Framework model based on an existing database in ASP.NET Core
但是,我看到的问题是,首先在组织上,如果该数据库有几百个表怎么办?我会有一堆模型文件。我知道有几K桌的应用程序。那将会看到只是淹没真正的应用程序而且......好像也好像过度杀人。
答案 0 :(得分:0)
实体框架不太适合这种情况。
我的建议是使用普通的ADO.NET。