数据库未显示在SQL Server资源管理器中

时间:2016-08-04 03:03:33

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

我正在尝试将实体框架用于update-database

它运行正常,没有错误。当我使用-Verbose显示

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

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

为什么它使用.\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>

1 个答案:

答案 0 :(得分:1)

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

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

此致