【问题标题】:Changed connection string but mvc application still using localdb更改了连接字符串,但 mvc 应用程序仍在使用 localdb
【发布时间】:2023-03-18 15:20:02
【问题描述】:

我将 web.config 中的连接字符串更改为使用 sqlexpress 而不仅仅是 localdb。我的应用程序仍然在 localdb 而不是 sqlexpress 中创建数据库。我被难住了。

这是我的连接字符串

Data Source=.\SQLEXPRESS;Initial Catalog=Jemco;Integrated Security=True;

可能是我的 app.config 的问题吗?

<?xml version="1.0" encoding="utf-8"?>
<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【问题讨论】:

  • 实体框架代码优先?查看(或在此处发布)初始化 DataContext 的行。
  • 是的,我首先使用实体​​框架代码。我使用工作单元和存储库来处理数据库,但与那里的连接无关。也许我不理解你的要求。

标签: sql-server asp.net-mvc


【解决方案1】:

您应该使用&lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"&gt; 而不是LocalDbConnectionFactory,并在其&lt;parameters&gt; 标签中添加连接字符串。

<?xml version="1.0" encoding="utf-8"?>
<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; Initial Catalog=Jemco;" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【讨论】:

    【解决方案2】:

    元素设置仅在您未向实体框架提供“提示”时适用。

    您可以重写 DbContext 基类构造函数以向实体框架提供“提示”。假设您在 .config 文件中定义连接字符串,您需要做的就是让 YOUR_DbContext 类调用提供连接字符串名称的基本 DbContext(string) 构造函数,将以下公共构造函数添加到 DbContext 类

     public class YOUR_DbContext : DbContext{
    
        public YOUR_DbContext()
            : base("**YOUR_ConnectionStringNameHere**"){
            //Optional - Write the actual connect string out to the Output window 
            Debug.WriteLine(Database.Connection.ConnectionString);
        }
    

    【讨论】:

      【解决方案3】:

      造成这种情况的另一个原因可能是当您在一个解决方案上有多个项目时,您选择了一个项目作为启动项目而不是您的 DBContext 项目,并且您没有在启动项目中设置连接字符串。只需设置启动项目中的连接字符串也是如此。

      希望能帮助别人;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多