Strange behavior with Entity Framework connection strings

c# connection-string entity-framework-6

Question

Okay, this is a weird one. I have a solution that contains two projects. Project A references Project B. Project B is, essentially, a class library project that contains an Entity Framework Model, created database first. In the app.config of Project A, I have a few connections strings that point to different databases on one server. My software works fine. Here are my connection strings:

<add name="LRIP 1, 2, 1A" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KREUS;initial catalog=Kreus2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />    
<add name="ECP 1" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KREUS;initial catalog=Kreus2ECP1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="ECP2" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KREUS;initial catalog=Kreus2ECP2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="TR-12" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KREUS;initial catalog=Kreus2TR_12;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Now, my coworker is also working on a solution that uses Project B (the data model). He has one connection string in his app.config file:

<add name="Kreus2Context" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KREUS;initial catalog=Kreus2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

As you can see, the connection string is pointing to the same server (Kreus) that mine are. He wanted to use my connections strings (since I already had them set up) and pasted them into his app.config.

Now, when he runs, he's getting an error:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development...

We're both using the same model (the one in Project B), so I'm not sure why my connection strings aren't working for him. There is no difference between the models, we're using it as a reference for the main project. Being relatively new to EF, I'm at a loss as to where to look for solutions. I'm hoping someone can point me in the right direction.

EDIT: For clarity, I should mention that my program lets the user pick from a drop-down and chooses the data table they want to interact with. I pass the chosen name into the constructor for the datacontext:

var dataContext = new Kreus2Context(name); // name is what the user chose for the drop-down

My co-worker added that same functionality (drop box, and constructor for context that takes a string parm for connection string name).

TL;DR: My co-worker and I are both using the same database first EF model. My connections strings cut and pasted into his app.config just do not work, even though we're using the same exact model (it's in a class library project that we both are referencing in our programs).

UPDATE: As pointed out in the answer below, the ultimate source of the problem here was that my co-worker copied the connection strings into the wrong app.config file. He had two projects in his solution, one for the GUI and one for the business logic, that used the Project B (EF model). The connection strings should have been applied to the app.config file in the GUI project, his executing project. Instead, they were copied to the app.config file in his business logic project, which wasn't even being used.

1
2
9/6/2014 5:52:20 PM

Accepted Answer

Do they point to the same project? If they both do the connection strings must reside in the controlling executable. (see No connection string named 'MyEntities' could be found in the application config file).

1
5/23/2017 12:10:41 PM

Popular Answer

Model first or database first calls on model creating to consume the .edmx file. Code generated by code first will throw the above exception in the on model creating method since code first does not use it.

  • a scan of the code will show if this method is returning the exception
  • if this is the case change the connection string to use the code first format


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow