Labels

Friday, August 21, 2009

PRB: Linq to SQL Database Connection String Issue

Hi,

While creating a DBML, if you’re Data Layer is a class library project then connection strings are stored in Settings File & App.Config File.

However if your need is to store the connection strings in Web.config, you need to do slight tweaking with the code.

  • Set the “Connection” Property for the “.dbml” designer file to “Null”
  • Create a partial class for your DataContext class and have the constructor with base constructor reading the connection string from web.config file

public partial class DataClasses1DataContext

{

public DataClasses1DataContext() : base (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)

{

OnCreated();

}

}

Make sure you have Connection string in your web.config.

Thats it. Simple !!! Happy Coding…

Update: The next time when you try to add a Stored Procedure or Table to the DataContext Designer, you still have to go to the properties of the DataContext and tell it not to read from “appsettings” and set the connection string to null/empty string. I wish there is a permanent way to say, do not populate these values.

Reference: Link

Thanks & Regards,

Arun Manglick Senior Tech Lead

No comments:

Post a Comment