【问题标题】:Database not showing in SQL Server ExplorerSQL Server 资源管理器中未显示数据库
【发布时间】:2016-08-04 03:03:33
【问题描述】:

我正在尝试使用实体框架来update-database

它运行良好,没有错误。当我使用-Verbose 时,它会显示

Target database is: 'PokemonAppCore.PkmnContext' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention)

但在app.config 中,我已将其指向本地数据库,就像这样。 LocalDb 下的 SQL Server Explorer 中没有显示任何内容。

为什么它使用.\SQLEXPRESS 而不是localdb,就像在app.config 中指定的那样?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" 
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                 requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="PkmnConnectionString" 
             connectionString="Data Source=(localdb)\v11.0;Initial Catalog=PkmnDatabase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"  
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
            <provider invariantName="System.Data.SqlClient" 
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
</configuration>

【问题讨论】:

  • 你是否将 PkmnConnectionString 传递给 DbContext 构造函数
  • 不,我以为我不必这样做,因为它是从 app.config 读取的?
  • 您必须将 PkmnConnectionString 传递给 DbContext

标签: c# sql-server entity-framework database-connection


【解决方案1】:

首先,正如 Mohammad Akbari 所提到的,在 DbContext 构造函数中传递连接字符串名称很重要。

我想补充一点,启动项目(在VS解决方案中)应该是使用EF的那个,update-database命令正在寻找启动项目的web.config(或app.config) .

问候,

【讨论】:

    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多