1

The Structure of the project :

There is a modules folder which I need to add the database and make the repository just there, Other modules and assemblies could use that. New : Changed it to previous proved state of placing the config file in the root application, still there are errors. This post is edited completely.


The Database Location :

location : ....\db\db.sdf (2 levels upper from the sell's app.config)

The App.Config file

location : modules\ModuleX\ (Formerly)

New Location : Shell\ (Main Project)

New Version :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyRecordzContext" connectionString="metadata=res://*...
  </connectionStrings>
</configuration>  

2 Cases of suspect : providerName="System.Data.EntityClient"

LocalDbConnectionFactory


Error Info :

Error Line : TblMyRecord MyRecord = context.MyRecords.First(w => w.MyRecord == stMyRecord);

Error : No connection string named 'MyRecordzContext' could be found in the application config file.


Goal : How to change the connectionstring , my app.config to make the application work as expected


Here EF 5.0, SQL CE , Prism, WPF, MVVM, C# were used


Error :

Error : The type initializer for 'System.Windows.Application' threw an exception.

Appears as the application runs.

The error seems to be before any build.

Stack Trace :

PresentationFramework.dll!System.Windows.Application.Application()
Xz.Shell.exe!Xz.Shell.App.App()
Xz.Shell.exe!Xz.Shell.App.Main()
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args)
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
[Native to Managed Transition]
LastBye
  • 1,143
  • 4
  • 19
  • 37

1 Answers1

2

Here's my hunch: ModuleX is the DAL project, which is referenced from another project (probably a Web, Windows Forms or Console application) that we'll call the Startup Project.

If this is the case, then the App.Config file in the DAL project is not used.

There are 2 solutions:

  1. You could specify the connection string in the Startup Project for it to be taken into account. This means that you need to add the <connectionStrings>...</connectionString> tag in:

    • web.config, if the Startup Project is a Web Application
    • app.config, otherwise (e.g. WPF, Console).
  2. Alternatively, you could define this connect string in the machine.config file and that will make it available for any .NET application running on that particular machine.

The connection string that's in the DAL project is not used at runtime. I think it's only used by the EF designer.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • At first I thought exactly like this, but I saw an exception in some code, unfortunately I can't remember where but In that code the main project was unaware. Really not sure how. But as a proved answer it could be OK to put the config in the main, but here the connection could have other simple faults. please look at my post's "2 Cases of suspect" section. Are they right ? having the .config in main, having the DB in the DAL project, the address I specified is correct ? Although I could test everything in several ways, some experienced hints could cut the time I should spend on this piece. – LastBye Oct 14 '12 at 19:04
  • Also I mentioned WPF, so don't think about the web. – LastBye Oct 14 '12 at 19:06
  • @Sypress the error, as you wrote in the question is: *"No connection string named 'MyRecordzContext' could be found in the application config file."* That's what lead to my answer – Cristian Lupascu Oct 14 '12 at 19:19
  • let's first get rid of this error and then we'll see if the connection string is correct – Cristian Lupascu Oct 14 '12 at 19:19
  • OK, WPF is just like Windows Forms - you need to put the connection string in app.config – Cristian Lupascu Oct 14 '12 at 19:20
  • thanks trying to help to find and solve the first error, I found EF5's connections are a bit different and fixed that, so the question subject is narrowed down, I did the approach you said still it didn't run. Edited the post to point to the problem. – LastBye Oct 14 '12 at 20:54
  • but still didn't help me. I just mentioned it's wpf, it's not the problem. – LastBye Oct 14 '12 at 20:57
  • OK, now the error is *The type initializer for 'System.Windows.Application' threw an exception*. Not sure if this is because of the connection string. Can you post more details about the error? Can you look at the stack trace and see at which line it fails? – Cristian Lupascu Oct 14 '12 at 21:07
  • @Sypress try running the program in debug mode. This way the stack trace will indicate the exact line of code where the error occurs – Cristian Lupascu Oct 15 '12 at 07:34
  • It's in debug mode already, I think may be there could be a problem in the Config elements – LastBye Oct 15 '12 at 07:51
  • After Disabling a debugging option (JMC) As I said. The error pointed to first running line on the main : Xz.Shell.App app = new Xz.Shell.App(); – LastBye Oct 15 '12 at 08:10
  • No, The problem is related to adding connection and entity tags to the config. It's related to EF5 and prism may be. thanks for being with me and sharing your ideas I have already given you a +1. – LastBye Oct 15 '12 at 08:12
  • @Sypress I know, but this is really weird. Let's try one more thing: show me your config file (of course, strip out any sensitive info). Just to check the XML is well formatted and according to the schema – Cristian Lupascu Oct 15 '12 at 08:16
  • At the time I said I changed the config here to the latest version – LastBye Oct 15 '12 at 10:50
  • @Sypress yes, sorry. Perhaps you could try moving the `` tag after the `` tag - I think the order matters and `` has to come first – Cristian Lupascu Oct 15 '12 at 10:59
  • yes, I found that, when I came to say that found your post. Found also The first post was absolutely right and the issues were caused by prism and EF5, and I some times have problems with relative URLs, The things work fine if I take myself away from my desired coding, when put everything in the main project. Now found the real cause of it and the possible solution. I should use some new features which will need some advice on them also and will ask more specific on them when needed. – LastBye Oct 15 '12 at 18:14
  • I understand this post is closed off. But I hit this problem when working with MVC4 Application. I am trying to separate my controllers and Models into two different project and the controller project is having problem finding the connection string (which resides inside the model project). I keep getting the `no connection string found in application config file`. I tried solution 1 of suggested in the answer but it is not working. @Sypress How did you get this fixed? – Steven Oct 24 '12 at 17:46
  • @Steven the Connection String must be specified in the MVC project's web.config file – Cristian Lupascu Oct 24 '12 at 20:14
  • @w0lf Yea, I found this through trial and error right after I posted the comment. It seem to kinda defeat the concept of MVC, as I had to put the connection string inside the UI layer and not the Controller layer, which does the actual reference to the context. But, I guess that is a conversation for another question/forum. And thanks for the reply. – Steven Oct 25 '12 at 14:24