2012-04-26 155 views
2

以下連接字符串一切正常。EF代碼優先使用SQL Server Express 2012連接字符串

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

我最近在我的本地機器上測試安裝了SQL Server 2012 Express,但我不能進行連接。這是使用Windows身份驗證的連接字符串。

<add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /> 

我是誰盡了最大努力搜索論壇共小白,但我無法推遲從部分「類似的標題問題,」我的解決方案。任何幫助是極大的讚賞。

回答

9

的EntityFramework代碼首先使用defaultConnectionFactory中的應用程序。 。的.config或web.config文件,它會自動連接到\ SQLEXPRESS這個配置是這樣的:

<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/> 
    </entityFramework> 

如果你使用EF代碼首先,你可能有自的DbContext派生的類來使用通過設計,EntityFramework將查找具有t的連接字符串他與您的上下文類名稱相同,並使用該名稱而不是defaultConnectionFactory。這是我如何使用它:

<connectionStrings> 
    <add name="ObjectContext" 
     connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=My.Db.Name; Integrated Security=True; MultipleActiveResultSets=True" 
     providerName="System.Data.SqlClient"/> 
    </connectionStrings> 

什麼我的建議是檢查EF不使用它的defaultConnectionFactory甚至強制覆蓋,通過增加您的上下文命名的連接字符串。 您還應該檢查this blog article,它提供了有關EF配置文件的詳細信息。

+0

我很抱歉花了這麼長時間來標記這是我的解決方案。我跟蹤另一個愛好(如果你已婚的話,是老大),現在剛剛回來了。我的連接字符串被覆蓋,發佈後我無法抓住它。 – Budoray 2013-05-07 17:43:04

0

您確定這部分連接是正確的嗎? 「數據源= \ SQLEXPRESS [名];

我沒有使用SQL Server 2012中,但儘量去除【名稱】文本

+0

我的歉意。連接字符串如下。 connectionString =「Data Source =。\ SQLEXPRESS; Catalog = myHub; Integrated Security = SSPI;」 providerName =「System.Data.SqlClient」 – Budoray 2012-04-27 14:25:48

+0

我也試過connectionString =「Data Source =。\ SQLEXPRESS; Integrated Security = SSPI;」 providerName =「System.Data.SqlClient」 – Budoray 2012-04-27 14:39:56

0

這爲我工作

add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS; 
Initial Catalog=Movies; 
Integrated Security="True" providerName="System.Data.SqlClient" 

我是通過本教程的工作,不得不去解決它。該MovieDBContext在代碼中創建在MVC應用程序

public class MovieDBContext : DbContext 
{ 
    public DbSet<Movie> Movies { get; set; } 
} 

這將自動創建IDE控制器類的模型,但它不會工作,直到我得到了連接字符串就在web.config文件。

相關問題