如何将localDb与实体框架迁移一起使用?

时间:2014-05-06 08:02:41

标签: c# entity-framework ef-migrations localdb

我有Persistence类库,其中包含我的DbContext类。它还包含app.config文件,其中我有预定义的连接字符串,如下所示:

  <connectionStrings>
    <add name="<Namespace1>.Persistence.AssessmentContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AssessmentContext.mdf;Integrated Security=True"
   providerName="System.Data.SqlClient" />
  </connectionStrings>

假设AssesmentContext有Namespace1.Persistence命名空间。

当我尝试使用PM控制台启用迁移时,它会给我一个错误:

An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.

当我尝试调试它并将调试输出放入AssessmentContext ctor时,我发现连接字符串仍在使用。\ SQLEXPRESS数据源。

Data Source=.\SQLEXPRESS;Initial Catalog=<Namespace1>.Persistence.AssessmentContext;Integrated Security=True;MultipleActiveResultSets=True

那么我做错了什么?为什么EF不从app.config获取我的连接字符串?

1 个答案:

答案 0 :(得分:0)

如果您使用的是桌面应用程序,则必须将上述连接字符串条目复制到可执行程序的app.config中;如果您使用的是Web应用程序,则必须将其复制到web.config。