自动生成的默认连接字符串与手动添加的默认字符串 [英] Autogenerated default connection string vs. manually added one

查看:94
本文介绍了自动生成的默认连接字符串与手动添加的默认字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个简单的WPF应用程序,它使用Entity Framework Code First创建数据库,连接到数据库并显示一些数据。从一开始我就不需要担心连接字符串,因此在通过Nuget添加了objectframework引用之后,我将获得如下所示的自动生成的app.config:

Let's say I have got simple WPF application using Entity Framework Code First to create database, connect to it and display some data. From start I do not want to worry about connection strings so after adding entityframework reference via Nuget I'll get auto generated app.config looking like this:

<?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" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      </providers>
  </entityFramework>
</configuration>

我将运行测试并观察连接字符串:

I'll run test and observe connection string:

var strings = ConfigurationManager.ConnectionStrings;

结果为:

[0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}

我想定义自己的连接字符串,因此将其添加到我的app.config中:

As I Like to define my own connection string, I will add this into my app.config:

<connectionStrings>
    <add name="MyContext" connectionString="data ource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

因此,当我再次运行测试并观察连接线时,我可以看到现在有两个:

And so when I run the test again and observe the connection srings I can see that there are two now:

[0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
[1] = {data source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True}

为什么我可以看到两个连接字符串?如果第一个是默认值,创建我是否应该不放弃?

Why is it that I can see two connection string? If the first one is default, should it not be forgoten once I've created one?

谢谢

推荐答案

您看到的第一个连接字符串来自 machine.config 。它具有以下部分:

First connection string which you see comes from machine.config from your PC. It has following section:

<connectionStrings>
     <add name="LocalSqlServer" 
          connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" 
          providerName="System.Data.SqlClient"/>
</connectionStrings>

其中哪个定义了ASP.NET数据库的默认连接字符串。如果您的应用程序确实不需要它,则可以编辑 machine.config 文件(不推荐)或清除连接字符串,然后再添加您的字符串:

Which defines default connection string for ASP.NET database. If you really don't need it for your application, you can either edit machine.config file (not recommended) or clear connection strings before adding yours:

<connectionStrings>
    <clear />
    <add name="MyContext" 
         connectionString="data source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

还请记住-实体框架不使用此连接字符串。默认情况下,它使用名称等于DbContext类全名的SQLEXPRESS服务器和数据库。您可以通过访问 context.Database.Connection.ConnectionString 来检查它。

Also keep in mind - this connection string is not used by Entity Framework. By default it uses SQLEXPRESS server and database with name equal to full name of your DbContext class. You can check it by accessing context.Database.Connection.ConnectionString.

这篇关于自动生成的默认连接字符串与手动添加的默认字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆