In a C# 2010 web application, I used linq to sql to setup my database connections. Now when I move the application to a different database, the original database is still being used. Basically the connection string is hard coded into the application.
The part that says, "1.Open up the LINQ to SQL designer, and open the Properties tab of the designer (the schema itself), expand Connection and set Application Settings to False. ", I did not see this option. The closest thing I found was connection and
I set that value.
Here is the way the code looks now in the *designer.cs file.
namespace e_ClScripts
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="DEV")]
public partial class eDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void Inserte_Detail(e_Detail instance);
partial void Updatee_Detail(e_Detail instance);
partial void Deletee_Detail(e_Detail instance);
partial void InsertIBook(IBook instance);
partial void UpdateIBook(IBook instance);
partial void DeleteIBook(IBook instance);
partial void InsertIPackage(IPackage instance);
partial void UpdateIPackage(IPackage instance);
partial void DeleteIPackage(IPackage instance);
partial void InsertIError_Tran(instance);
partial void UpdateIError_Tran(IError_Tran instance);
partial void DeleteIError_Tran(IError_Tran instance);
partial void InsertTransaction_Type(Transaction_Type instance);
partial void UpdateTransaction_Type(Transaction_Type instance);
partial void DeleteTransaction_Type(Transaction_Type instance);
partial void Inserte_Tracking(e_Tracking instance);
partial void Updatee_Tracking(e_Tracking instance);
partial void Deletee_Tracking(e_Tracking instance);
#endregion
public eDataContext() :
base(global::e_ClScripts.Properties.Settings.Default.DEVConnectionString, mappingSource)
{
OnCreated();
}
public eDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public eDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public eDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public eDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
Here is what the web.config file looks like right now:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="e_ClScripts.Properties.Settings.DEVConnectionString"
connectionString="Data Source=instance1\DEV;Initial Catalog=dev3;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Can you tell me or show me in code how to fix my problem so the application does not use the hard-coded values that were setup by linq to sql?
Basically the connection string is hard coded into the application.
Hi,
No, you cannot hard code the connection string for LINQ-TO-SQL, and you have to read the connection string from the web.config. Generally speaking, I think LINQ should do that for you and it's reading the connection string from web.config.
In fact you can use something like this to read connection string directly from web.config:
public dbDataContext() :
base(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString)
{
………………
}
wendy elizab...
Member
321 Points
390 Posts
C# web linq to sql default problem
Nov 07, 2012 10:38 PM|LINK
In a C# 2010 web application, I used linq to sql to setup my database connections. Now when I move the application to a different database, the original database is still being used. Basically the connection string is hard coded into the application.
I tried to follow the linq listed below, but everything did not work. http://goneale.com/2009/03/26/untie-linq-to-sql-connection-string-from-application-settings/
The part that says, "1.Open up the LINQ to SQL designer, and open the Properties tab of the designer (the schema itself), expand Connection and set Application Settings to False. ", I did not see this option. The closest thing I found was connection and I set that value.
Here is the way the code looks now in the *designer.cs file.
namespace e_ClScripts { using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System; [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="DEV")] public partial class eDataContext : System.Data.Linq.DataContext { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); #region Extensibility Method Definitions partial void OnCreated(); partial void Inserte_Detail(e_Detail instance); partial void Updatee_Detail(e_Detail instance); partial void Deletee_Detail(e_Detail instance); partial void InsertIBook(IBook instance); partial void UpdateIBook(IBook instance); partial void DeleteIBook(IBook instance); partial void InsertIPackage(IPackage instance); partial void UpdateIPackage(IPackage instance); partial void DeleteIPackage(IPackage instance); partial void InsertIError_Tran(instance); partial void UpdateIError_Tran(IError_Tran instance); partial void DeleteIError_Tran(IError_Tran instance); partial void InsertTransaction_Type(Transaction_Type instance); partial void UpdateTransaction_Type(Transaction_Type instance); partial void DeleteTransaction_Type(Transaction_Type instance); partial void Inserte_Tracking(e_Tracking instance); partial void Updatee_Tracking(e_Tracking instance); partial void Deletee_Tracking(e_Tracking instance); #endregion public eDataContext() : base(global::e_ClScripts.Properties.Settings.Default.DEVConnectionString, mappingSource) { OnCreated(); } public eDataContext(string connection) : base(connection, mappingSource) { OnCreated(); } public eDataContext(System.Data.IDbConnection connection) : base(connection, mappingSource) { OnCreated(); } public eDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public eDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } Here is what the web.config file looks like right now: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="e_ClScripts.Properties.Settings.DEVConnectionString" connectionString="Data Source=instance1\DEV;Initial Catalog=dev3;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: C# web linq to sql default problem
Nov 08, 2012 11:29 PM|LINK
Hi,
No, you cannot hard code the connection string for LINQ-TO-SQL, and you have to read the connection string from the web.config. Generally speaking, I think LINQ should do that for you and it's reading the connection string from web.config.
In fact you can use something like this to read connection string directly from web.config:
public dbDataContext() : base(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString) { ……………… }Nakor
Member
336 Points
60 Posts
Re: C# web linq to sql default problem
Nov 11, 2012 01:14 PM|LINK
see if http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/2616191e-ff7f-4520-8190-806fc7a104f7 helps