1

LINQ to SQL Connection Strings with class library projects

By default, creating a new LINQ to SQL model (.dbml) will put the connection strings in both the application settings file and also web.config / app.config. This is not so much of a problem for web projects, but what about class library projects? i have a connection class where I can use it to check connection in all pages but I have an error where it cant read DataContext at all.

This is a photo that shows my problem.

enter image description here

3 Answers 3

1

Generally speaking, class libraries don't support config files. There are ways to make it work, but it's not considered a good practice since different applications may use the same library to interact with different instances of the database. I would recommend looking at a dependency injection or inversion of control solution like Ninject to pass the connection string to the constructor from the app that references the library.

UPDATE:

If you absolutely must read a config file from an assembly instead of the calling application, it can be done with ConfigurationManager.OpenExeConfiguration(). There are several answers here on SO that provide code samples for doing so, but I'm not going to link to them because I strongly encourage you not to go down that road.

Sign up to request clarification or add additional context in comments.

1 Comment

thankx for your advice but now if want to complete how to solve this error ??
0

By the looks of it you're not using LINQ to SQL - all I can see is an EntityFramework edmx. Check your code generation strategy, and make sure you're trying to instantiate the correct context name (think it's whatever the Entity Container Name is set to).

Also you need to make sure System.Configuration is referenced.

3 Comments

my code generator is empty // Default code generation is disabled for model 'C:\Users\alaa\Documents\Visual Studio 2012\Projects\SocialNetworking\DataAccess\SocialNetworking.edmx'. // To enable default code generation, change the value of the 'Code Generation Strategy' designer // property to an alternate value. This property is available in the Properties Window when the model is // open in the designer.
OK, so there's plenty of documentation online on how to configure Entity Framework. In terms of this question, you can read configuration strings via System.Configuration.ConfigurationManager.
But, as mentioned in the answer above, if you're building a scalable app you should be looking at apply best practises incorporating DI/IoC.
0

You need to put the connection strings in the main application app.config

Just put a copy of the connection strings in there and you can access them or in this case web.config

Comments

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.