使用自定义的ConnectionString和DbProviderInfo创建DbContext [英] Creating DbContext with both custom ConnectionString and DbProviderInfo

查看:248
本文介绍了使用自定义的ConnectionString和DbProviderInfo创建DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有大量数据库共享相同的模式。它们分布在许多SQL Server上,一些是2005年,一些是2008年。当连接到2005服务器之后连接到一个2005服务器时,我们遇到了一个例外,首先说明如下:


更新条目时出错。有关详细信息,请参阅内部异常

更新条目时发生错误。查看内部例外情况。
正在使用的SQL Server版本不支持数据类型'datetime2'。


所以据了解,是根据2008年SQL服务器提供商信息创建的模型,因此当连接到2005服务器时,它不能使用DateTime2数据类型,因为它不支持。



我们希望针对2005服务器构建模型,但我不确定如何指定连接字符串和DbCompiledModel。以前我有几个构造函数使用连接字符串(包括其他参数)...

  public ProjectContext(string ConnectionString ):base(ConnectionString)
{

//获取与此DbContext相关的ObjectContext
var objectContext =(这作为IObjectContextAdapter).ObjectContext;

// ...使用ObjectContext

如何为所有上下文(最低公分母)指定针对2005服务器构建的编译模型,并指定连接字符串?我只是没有看到这样做的方式。



任何帮助将不胜感激。



编辑:
我们正在使用实体框架4.3代码,因此,没有一个设计时间的EDMX文件进行编辑。

解决方案

基类构造函数有一个重载!



我将OnModelCreating配置文件复制到一个返回名为BuildModel的DbCompiledModel的新方法中。当调用了具有ConnectionString构造函数的构造函数时,我获取了dbModel的值,这是一个私有readonly属性,如果为null,则将其设置为从BuildModel返回的值。属性值与连接字符串一起提供给基类​​构造函数。



我可以访问我要连接的SQL Server版本。最终我会把它引入并创建一个模型为2005年,一个在2008年。



我将离开这个开放一段时间,以防万一有更好的方法这个。

  public DbContext ProjectContext(string ConnectionString)
:base(ConnectionString,dbModel)
{

}

private DbCompiledModel _dbModel;
private DbCompiledModel dbModel;
{
get
{
if(_dbModel == null)
{
_dbModel = BuildModel(new DbModelBuilder());
}
return _dbModel;
}


private DbCompiledModel BuildModel(DbModelBuilder builder)
{
//一些配置东西
return builder.Build(new DbProviderInfo (System.Data.SqlClient,2005))
.Compile();
}


We have a plethora of databases all sharing the same schema. They are spread about on a number of SQL Servers, some 2005, some 2008. We are getting an exception when connecting to a 2005 server AFTER having connected to a 2008 server first stating the following:

An error occurred while updating the entries. See the inner exception for details. An error occurred while updating the entries. See the inner exception for details. The version of SQL Server in use does not support datatype 'datetime2'.

So as I understand it what is happening is the Model is being created according to a 2008 SQL servers provider info, and thus when connected to a 2005 server, it can't use the DateTime2 data type because that is not supported there.

We would like to build the model against a 2005 server, but I am unsure of how to specify both the connection string AND the DbCompiledModel. Previously I have had a couple of constructors that take the connection string (among other parameters)...

    public ProjectContext(string ConnectionString) : base(ConnectionString)
    {

        // Get the ObjectContext related to this DbContext
        var objectContext = (this as IObjectContextAdapter).ObjectContext;

        // ... does some stuff with the ObjectContext
    }

How can I specify the compiled model built against a 2005 server for ALL contexts (lowest common denominator) and also specify the connection string? I just don't see a way to do it at the moment.

Any help would be appreciated.

Edit: We are using Entity Framework 4.3 Code First, thus, there is not a design time EDMX file to edit.

解决方案

There is an overload for the base class constructor!

I copied my OnModelCreating configuration stuff into a new method that returns DbCompiledModel named BuildModel. When my constructor that has ConnectionString constructor is called, I get the value of dbModel, a private readonly property which if null, gets set to the value returned from BuildModel. The property value gets fed to the base class constructor along with the connection string.

I have access to the SQL server versions that I am connecting to. Eventually I will bring that in and create one model for 2005 and one for 2008.

I will leave this open for a while just in case there is a better way to do this.

public DbContext ProjectContext (string ConnectionString) 
    : base(ConnectionString, dbModel)
{

}

private DbCompiledModel _dbModel;
private DbCompiledModel dbModel;
{
    get
    {
        if (_dbModel == null)
        {
            _dbModel = BuildModel(new DbModelBuilder());
        }
        return _dbModel;
    }
}

    private DbCompiledModel BuildModel(DbModelBuilder builder)
    {
        //Some configuration stuff
        return builder.Build(new DbProviderInfo("System.Data.SqlClient", "2005"))
                      .Compile();
    }

这篇关于使用自定义的ConnectionString和DbProviderInfo创建DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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