1

I have created a class library and added a EF Model but as soon as I declare a variable my project just skip the rest of my code without any error. I do not understand what is causing this to happen.

Library Code

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 Code

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

When I create a instance of the library it does not hit my PhiscFile Method.

I have added a breakpoint to it and it stops at this constructor

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

I am using a windows application to test my library

14
  • Please edit your question and post the code surrounding ´live_restoreEntities db = new live_restoreEntities();´. Commented Dec 18, 2013 at 10:20
  • 1
    And what exactly is the problem now? What code gets skipped? Commented Dec 18, 2013 at 10:46
  • All of my code get skipped. It hits my declaration and then it just skip all my code. Commented Dec 18, 2013 at 11:03
  • WHAT code gets skipped? Where is it? Commented Dec 18, 2013 at 11:15
  • 1
    Then the problem should be something along the lines of the answer by Jens Kloster. Commented Dec 18, 2013 at 11:57

1 Answer 1

1

The parameterless constructor goes out and look for the conenctionstring in the App.config file. It look next to the .exe file.

I'm guessing that you need to include your App.config (from your entity library) to your WinForms library.

In the App.config, it should look like this:

<configuration>
  <connectionStrings>
    <add name="live_restoreEntities" 
         connectionString="<your connection string here>"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
Sign up to request clarification or add additional context in comments.

4 Comments

If this was the case, then the c'tor of the db context would throw an exception.
@ThomasWeller yes you are right. I believe that OP's is just not been shown the exception.. perhaps some IDE configuration could reviel it.
You should see it in the debugger anyway, right? Also, in case of an exception, the breakpoint inside the c'tor wouldn't be hit.
@ThomasWeller if the breakpoint is at the decalring line of the constructor, then it will "hit" that breakpoint, before continuening to the base("...") where I belive the exception is thrown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.