2013-12-18 79 views
1

我已經創建了一個類庫並添加了一個EF模型,但只要我聲明瞭一個變量,我的項目就會跳過我的代碼的其餘部分,而不會出現任何錯誤。我不明白是什麼導致了這種情況發生。將實體框架模型添加到類庫

庫代碼

public class Phisc 
{ 
    //Global DB Entity variable 
    live_restoreEntities db = new live_restoreEntities(); 

    //Write data to file 
    public void PhiscFile(int value) 
    { 
     string strFileName, strFilePath; 
     StreamWriter stm; 

     //Create a file name that will be created 
     strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_PHISC"; 
     //The path that were the file will be saved 
     strFilePath = "c:\\" + strFileName + ".txt"; 

     //Validate if file exists 
     if (!System.IO.File.Exists(strFilePath)) 
      System.IO.File.Create(strFilePath).Dispose(); 

     stm = new StreamWriter(strFilePath, false); 

     stm.Write("This is a test message from C#"); 

     stm.Close(); 
    } 
} 

WinForm的代碼

private void Form1_Load(object sender, EventArgs e) 
{ 
    Phisc.Phisc pFile = new Phisc.Phisc(); 
    pFile.PhiscFile(14); 
} 

當我創建它不打我的PhiscFile方法庫的實例。

我添加了一個斷點,並將其在此構造停止

public live_restoreEntities() : base("name=live_restoreEntities", "live_restoreEntities") 
{ 
    this.ContextOptions.LazyLoadingEnabled = true; 
    OnContextCreated(); 
} 

我使用的是Windows應用程序來測試我的圖書館

+0

請編輯您的問題,並張貼周邊'live_restoreEntities DB =新live_restoreEntities()的代碼;「。 –

+0

查看編輯的問題 – Gericke

+1

現在問題到底是什麼?哪些代碼被跳過? –

回答

1

參數的構造函數出去,並期待在該conenctionstring App.config文件。它看起來在.exe文件旁邊。

我猜你需要包括你的App.config(從你的實體庫)到你的WinForms庫。

在App.config,它應該是這樣的:

<configuration> 
    <connectionStrings> 
    <add name="live_restoreEntities" 
     connectionString="<your connection string here>" 
     providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
</configuration> 
+0

如果是這樣的話,那麼db上下文的c'tor會拋出異常。 –

+0

@ThomasWeller是的,你是對的。我相信,OP的只是沒有顯示出例外..也許一些IDE配置可能會改變它。 –

+0

無論如何,你應該在調試器中看到它,對吧?另外,如果發生異常,c'tor內部的斷點不會被觸發。 –