如何让Entity Framework在Unit Test项目中读取我的app.config文件

时间:2013-02-27 00:09:32

标签: entity-framework

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

<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的数据库,这就是连接字符串的样子:

  

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

这就是数据库创建/种子代码的样子(它在我运行任何测试之前运行一次):

    [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);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

MSDN article开始,您必须将连接字符串中的名称设置为DbContext的名称,然后“初始目录”参数将是您的数据库名称。例如:

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

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

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();
    }
}