实体框架4.3.1移民总是会调用默认的构造函数和忽略的ConnectionString [英] Entity Framework 4.3.1 migrations always invokes default constructor and ignores connectionstring

查看:113
本文介绍了实体框架4.3.1移民总是会调用默认的构造函数和忽略的ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行(code一)迁移使用我构建自己一个的connectionString。

该入口点是一个静态函数带有一个参数,ConnectionString中。

我有一个名为PocModel既具有默认的构造函数的DbContext类。

 公共PocModel():基地(PocModel)
{...
 

和与字符串参数的上下文构造:

 公共PocModel(字符串nameOrConnectionString):基地(nameOrConnectionString)
{...
 

我的目标是由ConnectionString中所针对的数据库上执行迁移,而不是ConnectionString中的EF神奇通过名存实亡(本地主机\ sqlex preSS - PocModel)造成的。

这个我没能做到。

我有一个外功能的声明是这样的:

 公共静态无效MigrateDatabase(字符串的connectionString)
{...
 

这个功能我试图实现在FLW。方式:

  DbMigrator迁移=新DbMigrator(新Migrations.Configuration());
 migrator.Configuration.TargetDatabase =新DbConnectionInfo(的connectionString,System.Data.SqlClient的);
 migrator.Update();
 

和这样的:

  Database.DefaultConnectionFactory =新SqlConnectionFactory(的connectionString);
 Database.SetInitializer&其中; PocModel>(新MigrationInitializer());
 PocModel模式=新PocModel(的connectionString);
 model.Dispose();
 

我甚至试图动态设置

  ConfigurationManager.ConnectionString [PocModel]
 

要在传递的connectionString。

但很可惜,所有的失败和默认PocModel构造从内部EF迁移code调用,目标定位服务器:localhost \ sqlex preSS和数据库:PocModel

我一直无法执行迁移到未命名PocModel居住在localhost \ sqlex preSS任何数据库。

这不是我使用一个app.config文件来设置我的connectionString,因为我需要通过静态外部函数传递这一个选项。

请帮忙,一直停留在这个问题上很长一段时间了。

编辑: 我已用这个技巧的工作,但我问的秩序的问题,以确认的确有没有其他解决问题的办法(又名EF迁移故障)

 私有静态字符串_databaseNameOrConnectionString =PocModel;

        内部静态字符串DatabaseNameOrConnectionString
        {
            {返回_databaseNameOrConnectionString; }
            // HACK:此二传手只能从SetupModule.MigrateDatabase调用。这是一个黑客绕过code-第一迁移使用此构造函数的。
            集合{_databaseNameOrConnectionString =价值; }
        }

        //在code首先迁移将调用此构造函数,而忽略了它的ConnectionString已经过去了。
        公共PocModel()
            :基地(DatabaseNameOrConnectionString)
        {
 

解决方案

更​​新数据库调用使用的是默认的构造函数,所以你必须修改功能。你提到你有一个静态类来做到这一点,我无法想出一个办法,使这项工作没有一个静态的帮手。

 公共静态类助手
    {
        公共静态字符串GetConnectionString()
        {
            SqlConnectionStringBuilder建设者=新SqlConnectionStringBuilder();
            builder.DataSource = @\ sqlex preSS。
            builder.InitialCatalog =migrateme;
            builder.IntegratedSecurity = TRUE;
            builder.MultipleActiveResultSets = TRUE;
            返回builder.ConnectionString;
        }
    }
 

和,而不是调用基类的构造,然后,我调用自定义字符串的构造函数,它创建了正确的数据库

 公共PocModel():基地(Helper.GetConnectionString())
{

}
 

I want to execute (code-first) migrations using a connectionString that I have constructed myself.

The entry point is a static function with one parameter, the connectionString.

I have a DbContext class called PocModel with both a default constructor.

public PocModel() : base("PocModel")
{ ...

And a context constructor with a string argument:

public PocModel(string nameOrConnectionString): base(nameOrConnectionString)
{ ...

My goal is to perform migrations on the database that is targeted by the connectionString, not the connectionString that EF "magically" creates via the name only (localhost\sqlexpress - PocModel).

This I have not been able to do.

I have an "outer" function declared like this:

public static void MigrateDatabase(string connectionString)
{ ...

This function I have tried to implement in the flw. ways:

 DbMigrator migrator = new DbMigrator(new Migrations.Configuration());
 migrator.Configuration.TargetDatabase = new  DbConnectionInfo(connectionString, "System.Data.SqlClient");
 migrator.Update();

And like this:

Database.DefaultConnectionFactory = new SqlConnectionFactory(connectionString);
 Database.SetInitializer<PocModel>(new MigrationInitializer());
 PocModel model = new PocModel(connectionString);
 model.Dispose();

I have even tried to dynamically set the

ConfigurationManager.ConnectionString["PocModel"]

To the connectionString passed in.

But alas, all fails and the default PocModel constructor is invoked from within the EF migration code, targetting server:localhost\sqlexpress and database:PocModel.

I have not been able to perform migrations to any database not named "PocModel" residing on "localhost\sqlexpress".

It is not an option for me to be using an app.config file to set my connectionString, as I need to pass this in via the static outer function.

Please help, been stuck on this problem for a very long time now.

EDIT: I have made it work by this hack, but I am asking the question in order to verify that there indeed is no other solution to the problem (aka the EF migrations is faulty)

  private static string _databaseNameOrConnectionString = "PocModel";

        internal static string DatabaseNameOrConnectionString
        {
            get { return _databaseNameOrConnectionString; }
            //HACK: This setter must ONLY be called from SetupModule.MigrateDatabase. It is a hack to circumvent the code-first migrations usage of this ctor.
            set { _databaseNameOrConnectionString = value; }
        }

        //the code first migrations will call this ctor and ignore the connectionString it have been passed.
        public PocModel()
            : base(DatabaseNameOrConnectionString)
        {

解决方案

The Update-database call is using the default constructor, so you have to modify that function. You mentioned you have a static class to do this, I couldn't figure out a way to make this work without a static helper.

 public static class Helper
    {
        public static string GetConnectionString()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = @".\sqlexpress";
            builder.InitialCatalog = "migrateme";
            builder.IntegratedSecurity = true;
            builder.MultipleActiveResultSets = true;
            return builder.ConnectionString;
        }
    }

and then instead of calling the base constructor, I called the constructor with the custom string, and it created the correct database

public PocModel() : base(Helper.GetConnectionString())
{ 

}

这篇关于实体框架4.3.1移民总是会调用默认的构造函数和忽略的ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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