将实体框架模型添加到类库

时间:2013-12-18 10:13:46

标签: c# entity-framework

我已经创建了一个类库并添加了一个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应用程序来测试我的库

1 个答案:

答案 0 :(得分:1)

无参数构造函数会在App.config文件中查找conenctionstring。它看起来就像.exe文件。

我猜你需要将你的App.config(从你的实体库)包含到你的WinForms库中。

在App.config中,它应如下所示:

<configuration>
  <connectionStrings>
    <add name="live_restoreEntities" 
         connectionString="<your connection string here>"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
相关问题