我已經使用EF 4.1和DB第一種方法創建了一個新的MVC 3網站。我創建了edmx文件,然後創建了DBContext(新的4.1特性)類。一切順利。使用Database First方法後,實體框架4.1將無法連接到SQL Server
然後,我使用DBContext的自動創建功能創建了新的控制器。一切都不會很好。
現在,我試圖啓動網站,它不會連接到自己創建的連接字符串。
這裏是連接字符串:
<add name="PizzaByMeEntities"
connectionString='metadata=res://*/Models.PizzaByMeModel.csdl|
res://*/Models.PizzaByMeModel.ssdl|
res://*/Models.PizzaByMeModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=(local);
initial catalog=PizzaByMe;
integrated security=True;
pooling=False;
multipleactiveresultsets=True;
App=EntityFramework;"'
providerName="System.Data.EntityClient" />
當我火起來的網站,我得到:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
奇怪的是,如果我刪除的連接字符串 - 我仍然得到確切同樣的錯誤。我猜EF即使在那裏也找不到連接字符串。
任何想法?
謝謝!
編輯:
我的DbContext如下:
public partial class PizzaByMeEntities : DbContext
{
public PizzaByMeEntities()
: base("name=PizzaByMeEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<X> Xs{ get; set; }
public DbSet<Y> Ys{ get; set; }
public DbSet<Z> Zs{ get; set; }
}
編輯2:
工作連接字符串是這樣的:
<add name="PizzaByMe" connectionString="Data Source=(local);initial catalog=PizzaByMe;integrated security=True;pooling=False;multipleactiveresultsets=True;" providerName="System.Data.EntityClient" />
雖然,使其工作 - 我只是使用直接的ADO.NET連接。
我建議你第一件事是驗證此連接字符串是確定的(提供連接字符串,而不是整個實體之一)。也許將它轉儲到簡單的SqlConnection/SqlCommand/SqlDataReader周圍的測試頁面或應用程序中,然後查看它是否連接。如果沒有,那麼你的問題就在那裏。如果沒關係,那麼至少你知道問題出在EF或你的代碼上,而不是你的數據庫連接字符串。 –
我檢查了它 - 工作得很好。 – Roman
您運行的是哪個版本的SQL Server? – itsmatt