2010-05-25 89 views
3

我有一個數據庫(SQL 2008 mdf文件),一個帶有edmx文件的類庫項目,使用嚮導創建。所以連接字符串也由嚮導完成。 該項目位於teamfoundation服務器上。連接字符串和實體框架問題

編碼時我可以使用所有嚮導製作的對象。

但是當我運行該程序,我試圖使entityContainerName,程序崩潰,並給出了這個錯誤:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

在這條線: 公共TestEntities():基地(「NAME = TestEntities」 ,「TestEntities」)

我該如何解決這個問題,或者我做錯了什麼?

回答

5

您需要將連接字符串從類庫的配置文件複製到實際運行的應用程序的配置文件中。

1

第一:刪除你的模型,第二:刪除你的app.config中的所有連接字符串。

然後重新創建模型。這應該將所有內容都恢復爲默認值。

+0

+1爲快速修復:-) – Stimul8d 2010-10-20 08:17:31

0

好的,如果你想在類庫中使用EntityFramework,這意味着你的應用程序不應該知道數據庫配置。這是我的觀點。我如何解決這個問題。 我們從所有App.Config文件中刪除所有連接字符串。 我們使用已創建的連接實例化我們的上下文。

public class ExpertDataBase : DbContext 
{ 
    public DbSet<Operator> Operators { get; set; } 

    public ExpertDataBase() 
     : base(ConnectionFactory.GetCESqlConnection(),true) 
    { 

    } 
} 

public static class ConnectionFactory 
{ 
    public static DbConnection GetCESqlConnection() 
    { 
     DbConnection connection = new SqlCeConnection() 
      { 
       ConnectionString = @"Data Source= ", 
      }; 
     return connection; 
    } 
} 

這是我的實現,它的工作原理沒有我在任何xml中輸入任何連接字符串。這個connectionFactory可以被重構,所以我可以以某種方式被注入並使用UnityContainer來解析連接類型。 希望這有助於