同时在不同的服务器上创建具有相同结构的数据库 [英] Create databases with same structure on different servers simultaneously

查看:172
本文介绍了同时在不同的服务器上创建具有相同结构的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要几个数据库(例如MySQL,MSSQL,Firebird)和相同的结构。如何同时创建具有相同模式的数据库,所以我不会浪费每个数据库的时间?
还如何将它们映射到一个实体模型(使用一个* .edmx模型)?或者至少在模型之间共享生成的类型?



我开发使用C#。感谢提前!



编辑1 :我发现这个问题,但没有回答同时使用实体框架与SQL Server和SQLite数据库

解决方案

如果你有上下文指向不同的数据库。那么你应该提供
连接信息,而不是使用App.config ConnectionStrings。



你的内容应该有一个构造函数,允许CONNECTION信息。
它仍然可以有旧的方式。



public abstract class YOURDbContext:DbContext,IContextOptions {

  // ctors 
protected YOURDbContext(string connectionName)
:base(connectionName){
}
//你需要这个构造器
protected YOURDbContext(DbConnection dbConnection,bool contextOwnsConnection)
:base(dbConnection,contextOwnsConnection){
}

每个上下文的实例都可以连接到不同的数据库。您一次有超过1个上下文。



您将需要一个功能 PER数据库类型,以确保connectionInfo正确构造。 p>

这里是获取SQL Server 的DBConnection的示例。

  public DbConnection GetSqlConn4DbName(string dataSource,string dbName){
var sqlConnStringBuilder = new SqlConnectionStringBuilder();
sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource)? DefaultDataSource:dataSource;
sqlConnStringBuilder.IntegratedSecurity = true;
sqlConnStringBuilder.MultipleActiveResultSets = true;

var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
var sqlConn = sqlConnFact.CreateConnection(dbName);
return sqlConn;
}


I tried to google and then searched some already answered questions on stackoverflow, but with no success.

I need several databases (e.g. MySQL, MSSQL, Firebird) with same structure. How do I create databases with same schema simultaneously, so I don't waste time for each? And also how do I map them to one Entity Model (with one *.edmx model)? Or at least share generated types between models?

P.S. I develop using C#. Thanks in advance!

Edit 1: I found this question, but no answer Using entity framework with both SQL Server and SQLite databases simultaneously

解决方案

If you which to have contexts point to different Databases. then you should provide the connection information and not use App.config ConnectionStrings.

YOUR content should have a constructor that allows CONNECTION info. It can still have the old way there.

public abstract class YOURDbContext : DbContext, IContextOptions {

    //ctors
    protected YOURDbContext(string connectionName)
        : base(connectionName) {
    }
    // YOU NEED THIS CONSTRUCTOR
    protected YOURDbContext(DbConnection dbConnection, bool contextOwnsConnection)
        : base(dbConnection, contextOwnsConnection) {
    }

Each instance of thr context can be connected to a different database. You have more than 1 context at once.

you will need a Function PER database type to make sure the connectionInfo is constructured properly.

here is an Example get the DBConnection for SQLServer. Repeat for other providers.

     public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
        var sqlConnStringBuilder = new SqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;

        var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    } 

这篇关于同时在不同的服务器上创建具有相同结构的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆