1

我正在尝试为我的实体框架测试创建一个数据库,但我无法让它正确读取我的配置文件。我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,连接字符串如下所示:

“数据源=(localdb)\v11.0;初始目录=Local.Model.LocalSqlContext;集成安全=True;MultipleActiveResultSets=True;应用程序名称=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);
            }
        }
    }
4

1 回答 1

2

从这篇MSDN 文章中,您必须将连接字符串中的名称设置为您的名称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();
    }
}
于 2013-02-28T21:23:21.240 回答