I got two project in my solution in Visual Studio 2010.
Project 1 contains an app.config with a ConnectionString.
How can I access that ConnectionString from Project 2?
Since they are both using the same ConnectionString, I would prefer to only have one instance in my solution.
You can add the files as a link to the file to one of your projects.
Use Add an Existing Item to the project you want to add the file to - the Open button has a small down arrow, if you click that you can select Add as Link.
This will add the file as a link from the original location, meaning you only have one physical file.
Another idea is to use an IoC container, like Ninject, injecting the connection-string into the constructor of the class you need it in. This is not unlike the factory pattern.
Note: you don't need to be using Entity Framework to use Ninject.
See this post:
Ninject - binding constructors with arguments / Entity Framework connection string
Beside of the file linking suggested in the answer by Oded, you may consider refactoring your application to use a commom data access assembly that contains a DatabaseConnectionFactory class or the like. This assembly would contain the connection string in its settings
If there is only specific section you'd like to share (connectionStrings in your case) then linking wouldn't work for you. Instead you could do something like this:
The solution is to store the connection strings on the web.config of the parent web app.
Note that the web site root is also an app, so you can store a web.config in there (i.e. c:\inetpub\wwwroot\web.config) which will be inherited by all apps under it.
c:\inetpub\wwwroot\web.config -> common configuration.
c:\inetpub\wwwroot\app1\web.config -> configuration for app1
c:\inetpub\wwwroot\app2\web.config -> configuration for app2.
In the case the default web site root is off limits, you can create a root app to contain all other apps and store the common configuration there.
c:\inetpub\wwwroot\myrootapp\web.config-> common configuration. c:\inetpub\wwwroot\myrootapp\app1\web.config -> configuration for app1 c:\inetpub\wwwroot\myrootapp\app2\web.config -> configuration for app2.
If your Project 2 has a reference of Project 1 then Project1 may have a class with a ConnectionString property exposed.
By the way, the "class" may read the connection string from the app.config
Related
I'm trying to make a WPF desktop application that communicates with a database through EntityFramework. I've separated my solution into several projects: DTO, DAL, Service and WPFApp. I've set up my database context and repositories in the DAL project and I'm using the WPFApp as startup. Because of this, I'm getting an error when trying to add objects to my db: 'Format of the initialization string does not conform to specification starting at index 0.'
Do I have to set up my DbContext and ConnectionString in the Startup Project or is there another way to make this work?
When you add an entity framework database first model in a project there's a checkbox on the connection screen and it says something like add connection string to app.config.
All your stuff the connection depends on then appears in that particular project's own config.
These aren't the configs you're looking for.
Or at least not the config the exe will look in when your dll is loaded and runs in it's context.
Simplest fix:
Copy all the connection stuff out your project config into the main solution's config.
I have an older asp.net solution consisting of several projects. The data access layer is contained in a separate class library project while the frontend is in another project.
The data access project is using Application Settings (https://msdn.microsoft.com/en-us/library/a65txexh.aspx) for several settings, among others 3 connection strings. I have a /Properties/Settings.settings file which - when changed - results in an updated /app.config file.
My problem is that I haven't found any way to automatically change these settings when building and publishing the solution.
I know about web.config transformations (https://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx) and that Visual Studio offers the ability to create a so called "Config Transform". But as far as I know a class library doesn't have a web.config file and this menu option is not available for neither the Settings.settings file nor the app.config file.
Is there a way to automatically change the settings.settings file/app.config or is there a completely different best practise to provide connections strings to a class library?
EDIT: I should add to the above that the Data Access class library is using Datasets.
I'm using EntityFramework 4 in my WPF desktop-application (NS: MyCompany.MyProduct).
Now I want to create the same application in ASP.NET (NS: MyCompany.MyProduct2), with the exact same functionality... Hence I need to use the exact same database as the WPF application already does.
Additionally, I want to create a new executable (hence a new wpf project) on top of my primary WPF project, that also uses the same ConnectionString like the WPF / ASP.NET-Application, to display some reports.
So I figured out I'd need to share the .edmx-Model (NS: MyCompany.MyProduct.Models.DBModel.edmx) and the ConnectionString that is already persistent in the app.config of the WPF app or the web.config of the ASP.NET-App.
What is the best or recommended way to do this?
What is the best or recommended way to do this?
Create a class library project and put EF model in there and share it between your WPF/Web projects. The app.config file of a library project isn't picked up by the parent project therefore you will have to manually update your web.config file to add the ConnectionString section.
This approach allows you to share business logic between your WPF app & your web app. If they are essentially the same app but on different platforms, then you should only be re-implementing the UI - this is one of the major advantages of the MVC pattern.
Agree with #James here. Don't be afraid of adding library projects to your solution. So you would have a project called MyCompany.Model that contains your EDMX. (Actually, you might find later that you want to use the T4 generation to split your model off from your DbContext or ObjectContext, but that's another discussion.)
With Visual Studio you can actually add a project--your EDMX project--to more than one solution. Be careful not to make changes to the EDMX project when editing one solution that break the other, though.
Respectfully, you may find that it's not ideal to use the GAC here, especially if your EDMX is still evolving.
As for connection strings, these are one thing that you tend not to share between projects. Typically they are in the app.config (or web.config) for your executable project. This can be a gotcha, because if you use a library project to hold your EDMX, EF will automatically create an app.config in the library project, with the connection string in it. But .NET never uses an app.config for a DLL. The only reason it's there is to give you something you can copy/paste into the real app.config for your executable (WPF) app.config or the web.config.
If your goal is to share the single .edmx dll between all three applications on one machine, the best way to accomplish this is to sign the dll, then add it to the GAC. If the dll will remain on different servers, there is no need to GAC the dll, you can just reference it in your projects, and add the connectionstring entry in the respective .configs.
GAC: http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.100).aspx
Install a DLL to the GAC: http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx
I have one class Library Project for Payment. The live credential of client is on web.config file of website and this class library project will added in bin in form of dll. So How to get ConfigurationManager in Class Library So I can get credential of Client and make Payment
Generally a class in the library shouldn't be reading web.config directly and take settings in constructor.
However it is acceptable for library in some scenarios to read the web.config directly. For that you can define a custom configuration section or use WebConfigurationManager.AppSettings
You need to add reference to System.Web assembly and you need to include the namespace System.Web.Configuration in the file where you want to use configuration manager.
Add Reference System.Web to your Class Library.
It's better if you can move that class away from the class library but if you can't or you don't want to do it, then you can work with System.Configuration.
1) Add a reference to System.Configuration.
2) Use ConfigurationManager instead of WebConfigurationManager; the code will be almost the same, just replace WebConfigurationManager with ConfigurationManager.
I had this same issue, I am assuming you would like to use the ConfigurationManager to access some properties in your config file.
An alternative is to add a settings file to the project. That is what I have done in my project:
Right click on the project name, go to the Properties link on the menu.
Click on the settings link on the left tab
If you have not already generate one VS will give you a link to create one.
Once you have created one, you can add key value pairs for the properties you require.
VS will include a settings.settings file in your project which will contain the properties that you have added in the settings file (you should see them under Settings.Designer.cs assuming you are using C#)
You can then access these settings in your project's classes by doing [ProjectNamespace].Properties.Settigns.Default.[PropertyName]
move your class library configuration settings to web.config.
I have a website built in asp.net mvc 3 which uses the repository pattern and EF.
I have added another project to my solution and would like to access the repositories from within this project for the database work.
I have added a reference to my web project from my new project but when I try and instance a repository I get the error:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
I'm guessing that EF doesnt like to be called outside of the project that it is configured for.
Am I doing this wrong?
Thanks
.NET will use the Web.Config or App.Config from the startup project for configuration. You need to copy the connection strings (and any other necessary settings) into your new project for your database connections to work.
It is also a best practice to move the Repositories and .edmx file into a separate project so your new application does not depend on the entire Web project being correct before it works. You can create a new Class Library project in your solution, drag the Model folder in there, and then add a reference to this project in both your mvc 3 site and your new application.