对于实体框架,我非常新,所以如果这个问题是基本的,我很抱歉。App.Config中的实体框架连接字符串
我正在通过Code First教科书工作,并创建了一个小型类库(c#)和一个控制台应用程序。该教科书指出,当我运行它时,实体框架将自动在SQL服务器中构建数据库。它没有,我相信的原因是因为我有一个命名实例而不是默认实例\ SQLExpress。该教科书指出,在使用默认实例时,我不需要连接字符串,但由于我没有使用默认实例,所以我猜测我需要在App.Config中使用connectionm字符串。我曾尝试在此文件中放入标准连接字符串,但它不起作用。
这里是我的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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
可能有人请告知在连接字符串去,或者告诉我,如果我要对这个错误的方式。
UPDATE
感谢帮助迄今收到,我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>
<connectionStrings>
<add name="Model" connectionString="Server=OFFICE-ACER\SQLE;Database=BreakAway;Trusted_Connection=True;" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
然而,什么也没有发生,即没有创建数据库之前。
您需要一个连接字符串。此外,配置中连接字符串的名称是您在创建实例时传递给DBContext的内容。 – Darren