2013-05-08 95 views
0

我創建了一個新的模式:實體框架 - 「不正確的連接字符串」 錯誤

public class AuthorDatacontext : DbContext 
    { 
     public DbSet<Author> Authors { get; set; } 

     static AuthorDatacontext() 
     { 
      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AuthorDatacontext>()); 
     } 
    } 

連接字符串:

<add name="MyLibrary.Models.AuthorDatacontext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MyLibrary.Models.AuthorDatacontext.mdf" providerName = "System.Data.SqlClient" /> 

,我用它在我的控制器:

var db = new AuthorDatacontext(); 
       db.Authors.Add(author); 
       db.SaveChanges(); 

我之前做過,在另一個項目中,它運行良好。 但現在我收到一個錯誤,試圖保存新作者:

An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. 

我到底做錯了什麼? 如果我刪除連接,並只保留默認連接,一切工作正常。

以前的項目有相同的連接字符串:

<add name="MvcAuction.Models.AuctionsDataContext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MvcAuction.Models.AuctionsDataContext.mdf" providerName = "System.Data.SqlClient" /> 

回答

1

我認爲問題是,EF試圖從你的項目再次創建MDF。嘗試先刪除它。

\MvcAuction.Models.AuctionsDataContext.mdf EF試圖查看它是否存在於該位置。如果您使用過SQL服務器,它會將更改從CODE First遷移到數據庫。

+0

但它的另一個項目。它爲什麼尋找它? – 2013-05-08 09:22:09

+0

您需要一個數據庫來初始化您的EF上下文 – 2013-05-08 09:31:57

+0

我如何創建它?爲什麼我在第一個項目中沒有這個問題? – 2013-05-08 09:34:00