0

I have an app.config file in my project as shown below.

I have following code to read the connection string:

string connectionstring = ConfigurationManager.ConnectionStrings["LibraryReservationSystemEntities"].ConnectionString;

It is showing exception as listed below.

Object reference not set to an instance of an object.

How can we correct it?

Note: This is a class library project. I copied this connection string from another project which is having EMDX file for EF. I have only one project in my current solution.

Note: I need to instantiate a ObjectContext (of EF) from a my project. The EMDX is available in a different project.

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
<connectionStrings>
    <add name="LibraryReservationSystemEntities" connectionString="metadata=res://*/MyEDMtest.csdl|res://*/MyEDMtest.ssdl|res://*/MyEDMtest.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;AttachDbFilename=C:\DevTEST\Databases\LibraryReservationSystem.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
 </configuration>

REFERENCE

  1. How do I programmatically set the connection string for Entity-Framework Code-First?

  2. Best way to initialize an entity framework context?

Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

3 Answers3

1

You can't have a config file for a library project. This configuration file is related to the executed assembly, which could be a winform app, WPF app, console app, ASP.Net website in IIS...

Add your ConnectionStrings section in the app.config (or web.config) file of the actual assembly that is executed.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • This is my answer... (made 5 mins ago) – Paul Fleming Jul 18 '12 at 11:59
  • @flem Sorry but at the time I wrote my answer, yours was incomplete. You edited it later. – ken2k Jul 18 '12 at 12:03
  • When EF adds a EMDX it is adding a config file in its project. Why is it so? I need to instantiate a ObjectContext from a my project. The EMDX is available in a different project. – LCJ Jul 18 '12 at 12:07
  • 1
    @Lijo It is mainly used for the "update from DB" feature of the EDMX model. It doesn't represent the actual DB that'll be used. ObjectContext accept various connection string formats as input. – ken2k Jul 18 '12 at 12:09
  • Thanks. I am using the approach mentioned in http://stackoverflow.com/questions/6003085/how-do-i-programmatically-set-the-connection-string-for-entity-framework-code-fi – LCJ Jul 18 '12 at 12:37
0

Check this

you need to add a .dll reference for configuration manager

recheck if your connection string is right

check if you have more than one configuration file ex. web.config and app.config; so the ConfigurationManager is referencing the wrong file in the solution.
Peru
  • 2,871
  • 5
  • 37
  • 66
0

If ConnectionStrings["myConnection"] returns null, then dereferencing it to get the Name property will fail in the constructor. Is that definitely not where the bug is?

Why not put a breakpoint on that line and take a look in ConfigurationManager.ConnectionStrings to check what it thinks it's got - and check very carefully for typos.

After that, put a breakpoint on the first line of the method, and check what the value of connectionString is. Passing in null to the SqlConnection constructor doesn't actually throw an exception, but you'd get an InvalidOperationException when you try to open it.

Peru
  • 2,871
  • 5
  • 37
  • 66
  • I am not using any database connection. I am just trying to read the config value. The exception comes in this read itself. – LCJ Jul 18 '12 at 12:02