如何让单位测试项目中的实体框架读取我的app.config文件 [英] How to get Entity Framework to read my app.config file in Unit Test project

查看:81
本文介绍了如何让单位测试项目中的实体框架读取我的app.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的实体框架测试创建一个数据库,但是我无法正确读取我的配置文件。测试项目中的 app.config 文件如下所示:

I'm trying to create a database for my entity framework tests, but I can't get it to read my configuration files correctly. My app.config file in test project looks like this:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="TestConnection"
         connectionString= "Data Source=(LocalDb)\v11.0;
                            Initial Catalog=LocabalTestDB;
                            Integrated Security=SSPI;
                            MultipleActiveResultSets=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  ....
</configuration>

但是,它不是创建一个名为 LocabalTestDB ,它正在创建一个名为 Locabal.Model.LocabalSqlContext 的数据库,这是连接字符串的外观:

However, it is not creating a database called LocabalTestDB, it is creating a database called Locabal.Model.LocabalSqlContext, and this is what the connection string looks like:


Data Source =(localdb)\v11.0; Initial Catalog = Locabal.Model.LocabalSqlContext; Integrated Security = True; MultipleActiveResultSets = True; Application Name = EntityFrameworkMUE

"Data Source=(localdb)\v11.0;Initial Catalog=Locabal.Model.LocabalSqlContext;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE"

这是数据库创建/种子代码看起来像(运行任何测试之前运行一次):

This is what the DB creation/seed code looks like (it runs once before I run any tests):

    [AssemblyInitialize]
    public static void Init(TestContext c)
    {
        Database.SetInitializer<LocabalSqlContext>(null);

        using (var db = new LocabalSqlContext())
        {
            if (!db.Database.Exists())
            {
                db.Database.Create();
                Locabal.Model.Migrations.Configuration.SeedData(db);
            }
        }
    }


推荐答案

从这个 MSDN文章,您必须将连接字符串中的名称设置为您的 DbContext 的名称,那么初始目录参数将是您的数据库名称。例如:

From this MSDN article, you have to set the name in connection string to the name of your DbContext, then the "Initial Catalog" parameter will be your DB name. For example:

  <connectionStrings>
    <add name="LocabalSqlContext"
         connectionString= "Data Source=(LocalDb)\v11.0;
                            Initial Catalog=LocabalTestDB;
                            Integrated Security=SSPI;
                            MultipleActiveResultSets=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

然后你可以这样设置你的测试类:

Then you can set your test class like this:

public class TestDBSeeder : DropCreateDatabaseIfModelChanges<LocabalSqlContext>
{
    protected override void Seed(LocabalSqlContext context)
    {
        Locabal.Model.Migrations.Configuration.SeedData(context);
        base.Seed(context);
    }
}

[TestClass]
public class LocabalSqlContextIntegrationBaseTest
{
    protected TransactionScope Transaction { get; set; }

    [AssemblyInitialize]
    public static void Init(TestContext c)
    {
        Database.SetInitializer<LocabalSqlContext>(new TestDBSeeder());

        if (!Database.Exists("LocabalSqlContext"))
        {
            using (var db = new LocabalSqlContext())
            {
                db.Database.Initialize(true);
            }
        }
    }

    [TestInitialize]
    public virtual void StartDbTransaction()
    {
        Transaction = new TransactionScope();
    }

    [TestCleanup]
    public virtual void EndDbTransaction()
    {
        Transaction.Dispose();
    }
}

这篇关于如何让单位测试项目中的实体框架读取我的app.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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