Database Connection problem with MVC3 - asp.net

I am using MVC3 with Code first approach. In this case, I had to generate my entity classes from the existing databse.
The database was
Database1.mdf
Once I did that, it created DBEntities and added a new connectionstring in my Web.config which looked something like this:
<add name="DATABASE1Entities" connectionString="metadata=res://*/Models.Task.csdl|res://*/Models.Task.ssdl|res://*/Models.Task.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\DATABASE1.MDF;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Now I removed DB Entities and came up with my own DB Context class.
and
Now, I am working with the following connectionstring:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DATABASE1.mdf;User Instance=true" providerName="System.Data.SqlClient" />
Name of my DBcontext class is TaskContext.
I am not sure what happeend afer this. My code works. But it works on some blank database and it does not reflect anydata in database.mdf. If I add something using my controller then I see that thing is added. But it does not get reflected in Databse1.mdf.
It seeems to have created a its own databse. But I do not see andy .sdf or .mdf file created anywhere.... I am not sure what is going on?

Make the name of your connection string same as TaskContext:
<add name="TaskContext"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DATABASE1.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
OR, you can use your favorite connectionStringName by following these steps:
1- When you are creating a TaskContext object, changes it's connectionString:
var cn = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
// or
var cn = WebConfigurationManager.ConnectionStrings[0].ConnectionString; // use your connection index instead of 0
var _context = new TaskContext();
_context.Database.Connection.ConnectionString = cn;
2- in your context class (TaskContext), you should tell to modelBuilder to
remove IncludeMetadataConvention
by this code:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
base.OnModelCreating(modelBuilder);
}

Try making the name of your connection string and DBContext the same. I believe reading at some point that EF Code First employs a lot of conventions so if it doesn't find a connection string named as the DB Context (in your case "TaskContext") it will try to connect to a SQLExpress (whether or not there is one installed) and will try to find or create the database.
I'm assuming you've got SQLExpress installed. What's down in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\? (I think that's the path) Have you got a DB sitting there for this project? Maybe something like [YourNamespace].TaskContext.dbf?

Related

passwords for web.config in development and production [duplicate]

I'm trying to set up a connecting string in my web.config file (Visual Studio 2008/ASP.NET 3.5) to a local server (SQL server 2008).
In my web.config, how and where do I place the connection string?
Here's what web.config file looks like right now: http://imwired.net/aspnet/Online_web.config
You can also use this, it's simpler. The only thing you need to set is "YourDataBaseName".
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Where to place the connection string
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
</configuration>
For some reason I don't see the simple answer here.
Put this at the top of your code:
using System.Web.Configuration;
using System.Data.SqlClient;
Put this in Web.Config:
<connectionStrings >
<add
name="myConnectionString"
connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
and where you want to setup the connection variable:
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
I found this very difficult to get an answer to but eventually figured it out. So I will write the steps below.
Before you setup your connection string in code, ensure you actually can access your database. Start obviously by logging into the database server using SSMS (Sql Server Management Studio or it's equivalent in other databases) locally to ensure you have access using whatever details you intend to use.
Next (if needed), if you are trying to access the database on a separate server, ensure you can do likewise in SSMS. So setup SSMS on a computer and ensure you can access the server with the username and password to that database server.
If you don't get the above 2 right, you are simply wasting your time as you cant access the database. This can either be because the user you setup is wrong, doesn't have remote access enabled (if needed), or the ports are not opened (if needed), among many other reasons but these being the most common.
Once you have verified that you can access the database using SSMS. The next step, just for the sake of automating the process and avoiding mistakes, is to let the system do the work for you.
Start up an empty project, add your choice of Linq to SQL or Dataset (EF is good but the connection string is embedded inside of an EF con string, I want a clean one), and connect to your database using the details verified above in the con string wizzard. Add any table and save the file.
Now go into the web config, and magically, you will see nice clean working connection string there with all the details you need.
{ Below was part of an old post so you can ignore this, I leave it in for reference as its the most basic way to access the database from only code behind. Please scroll down and continue from step 2 below. }
Lets assume the above steps start you off with something like the following as your connection string in the code behind:
string conString = "Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;";
This step is very important. Make sure you have the above format of connection string working before taking the following steps. Make sure you actually can access your data using some form of sql command text which displays some data from a table in labels or text boses or whatever, as this is the simplest way to do a connection string.
Once you are sure the above style works its now time to take the next steps:
1.
Export your string literal (the stuff in the quotes, including the quotes) to the following section of the web.config file (for multiple connection strings, just do multiple lines:
<configuration>
<connectionStrings>
<add name="conString" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
<add name="conString2" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
<add name="conString3" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
{ The above was part of an old post, after doing the top 3 steps this whole process will be done for you, so you can ignore it. I just leave it here for my own reference. }
2.
Now add the following line of code to the C# code behind, prefrably just under the class definition (i.e. not inside a method). This points to the root folder of your project. Essentially it is the project name. This is usually the location of the web.config file (in this case my project is called MyProject.
static Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/MyProject");
3.
Now add the following line of code to the C# code behind. This sets up a string constant to which you can refer in many places throughout your code should you need a conString in different methods.
const string CONSTRINGNAME = "conString";
4.
Next add the following line of code to the C# code behind. This gets the connection string from the web.config file with the name conString (from the constant above)
ConnectionStringSettings conString = rootWebConfig.ConnectionStrings.ConnectionStrings[CONSTRINGNAME];
5.
Finally, where you origionally would have had something similar to this line of code:
SqlConnection con = new SqlConnection(conString)
you will replace it with this line of code:
SqlConnection con = new SqlConnection(conString.ConnectionString)
After doing these 5 steps your code should work as it did before. Hense the reason you test the constring first in its origional format so you know if it is a problem with the connection string or if it is a problem with the code.
I am new to C#, ASP.Net and Sql Server. So I am sure there must be a better way to do this code.
I also would appreicate feedback on how to improve these steps if possible. I have looked all over for something like this but I eventually figured it out after many weeks of hard work. Looking at it myself, I still think, there must be an easier way.
I hope this is helpful.
it should be within the <configuration> node:
<connectionStrings >
<add name="myconnectionstring" connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
</connectionStrings>
this site has more info on it:
Connection in WebConfig
Add the your connection string to the <connectionStrings> element in the Web.config file.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com" providerName="System.Data.SqlClient" />
</connectionStrings>
In Class.Cs
public static string ConnectionString{
get{
return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;}
set{}
in header
using System.Configuration;
in code
SqlConnection conn = new SqlConnection(*ConfigurationManager.ConnectionStrings["connstrname"].ConnectionString*);
You can also use external configuration file to specify connection strings section, and refer that file in application configuration file like in web.config
Like the in web.config file:
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
The external configuration connections.config file will contains connections section
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
Modifying contents of external configuration file will not restart the application (as ASP.net does by default with any change in application configuration files)
If you want to write connection string in Web.config then write under given sting
<connectionStrings>
<add name="Conn" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com"
providerName="System.Data.SqlClient" />
</connectionStrings>
OR
you right in aspx.cs file like
SqlConnection conn = new SqlConnection("Data Source=12.16.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com");
You can put this in your web.config file connectionStrings:
<add name="myConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
You can use following format:
<connectionStrings>
<add name="ConStringBDName" connectionString="Data Source=serverpath;Initial Catalog=YourDataBaseName;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
Most probably you will fing connectionstring tag in web.config after <appSettings>
Try out this.
You can try this. It is very simple
<connectionStrings>
<add name="conString" connectionString="Data Source=SQLServerAddress;Initial Catalog=YourDatabaseName; User Id=SQLServerLoginId; Password=SQLServerPassword"/>
</connectionStrings>
Try this for your connection string.
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;
I JUST FOUND!!
You need to put this string connection and point directly to your database.
Same case on server.
"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=c:/inetpub/wwwroot/TEST/data/data.mdb;"
It works!! :)
Store connection string in web.config
It is a good practice to store the connection string for your application in a config file rather than as a hard coded string in your code. The way to do this differs between .NET 2.0 and .NET 3.5 (and above). This article cover both.
https://www.connectionstrings.com/store-connection-string-in-webconfig/
Create a section called <connectionStrings></connectionStrings> in your web.config inside <configuration></configuration> then add different connection strings to it, for example
<configuration>
<connectionStrings>
<add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>
</connectionStrings>
</configuration>
Here's a list of all the different connection string formats https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx

SimpleMembershipProvider, Entity Framework and default database

I'm using in a MVC4 internet app the new SimpleMemebership provider with Entity Framework code first. Evrything is fine except that the framework creates the new database with named "DefaultConnection" even if I changed it.
<add name="MyDB" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=BeerOperator; Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
and in Filters\InitializeSimpleMembershipAttribute.cs
WebSecurity.InitializeDatabaseConnection("MyDB", "UserProfile", "UserId", "UserName", autoCreateTables: true);
I really don't understand what I need to change in order to have the User management table created into MyDB database.
Many thanks
davide
Nevermind, I found it.
In the AccountModel.cs I had:
public UsersContext() : base("DefaultConnection")
I changed into:
public UsersContext() : base("MyDB")
and now is ok.

Keyword not supported: 'server'

I've been trying to edit my connection string for uploading my website to a server.
I am not really experienced with this. I got this exception: the Keyword not supported: 'server'.
Here is my connection string:
<add name="AlBayanEntities" connectionString="Server=xx.xx.xxx.xxx,xxxx;Database=AlBayan;Uid=bayan;Password=xxxxx;" providerName="System.Data.EntityClient" />
I've tried embed this string into my old connection string which works very well locally, but it didn't fit : S
For Entity Framework (database-first or model-first; when you have a physical EDMX model file) you need to use a special type of connection string which is quite different from the straight ADO.NET connection strings everyone else has mentioned so far...
The connection string must look something like:
<add name="testEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=test;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
Inside this connection string, you'll find the provider connection string= attribute which is basically your ADO.NET connection string:
provider connection string="data source=(local);initial catalog=test;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
So here, you need to change your server name and possibly other settings.
data source=.... stands for your server (you can also use server=.....)
initial catalog=..... stands for your database (you can also use database=....)
In MVC5 using EntityFramework 6.xx and Code First Approach
I had the same problem and I solved this by modifying my providerName
from
providerName="System.Data.EntityClient"
to
providerName="System.Data.SqlClient"
This exception is thrown on azure websites when you store the connection string in the App Service itself (on the 'Application Settings' blade).
If the connection string is an Entity Framework connection string the quotes will be encoded as " by default in your web.config file.
You need to change these back to actual quotes so the connection string can be parsed properly.
I always either run a connection wizard to build my string or I use connectionstrings.com.
Assuming SQL Server:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Comparing to yours it is very different.
Server=xx.xx.xxx.xxx,xxxx;Database=AlBayan;Uid=bayan;Password=xxxxx;
Same happened to me when using Rider IDE for a .net core 2.2 project.
As Rider automatically sets up Sqlite in Startup.cs in the "ConfigureServices" method.
By changing
services.AddDbContext<ApplicationDbContext>(options =>
options.**UseSqlite**(
Configuration.GetConnectionString("DefaultConnection")));
into
services.AddDbContext<ApplicationDbContext>(options =>
options.**UseSqlServer**(
Configuration.GetConnectionString("DefaultConnection")));
issue was resolved.
Of course, first you have to install nuget packages for EF SQLServer and add the connection string to appsettings.json.
Try this
<add name="AlBayanEntities" connectionString="Data Source=xx.xx.xxx.xxx,xxxx;Initial Catalog=AlBayan;User Id=bayan;Password=1abcd;" providerName="System.Data.EntityClient" />
EntityConnectionStringBuilder bb = new EntityConnectionStringBuilder();
bb.Metadata = "res://*/dao.bdmi.csdl|res://*/dao.bdmi.ssdl|res://*/dao.bdmi.msl";
//same as below client tobe used
bb.Provider = "MySql.Data.MySqlClient";
MySql.Data.MySqlClient.MySqlConnectionStringBuilder mbb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder();
mbb.Server = "";
mbb.Database = "";
mbb.UserID = "";
mbb.Password = "";
mbb.PersistSecurityInfo = true;
//use providerconnectionstring insted of connectionstring
bb.ProviderConnectionString = mbb.ToString();
return bb.ToString();
Through this way, you can change your ConnectionString as you want.
In my case, I found that my project had the EFSQLLite nuget package installed, which seems doesn't recognise the Server= keyword.
I uninstalled that nuget and installed the full EFSQLSERVER one, and it worked fine

Setting up connection string in ASP.NET to SQL SERVER

I'm trying to set up a connecting string in my web.config file (Visual Studio 2008/ASP.NET 3.5) to a local server (SQL server 2008).
In my web.config, how and where do I place the connection string?
Here's what web.config file looks like right now: http://imwired.net/aspnet/Online_web.config
You can also use this, it's simpler. The only thing you need to set is "YourDataBaseName".
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Where to place the connection string
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
</configuration>
For some reason I don't see the simple answer here.
Put this at the top of your code:
using System.Web.Configuration;
using System.Data.SqlClient;
Put this in Web.Config:
<connectionStrings >
<add
name="myConnectionString"
connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
and where you want to setup the connection variable:
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
I found this very difficult to get an answer to but eventually figured it out. So I will write the steps below.
Before you setup your connection string in code, ensure you actually can access your database. Start obviously by logging into the database server using SSMS (Sql Server Management Studio or it's equivalent in other databases) locally to ensure you have access using whatever details you intend to use.
Next (if needed), if you are trying to access the database on a separate server, ensure you can do likewise in SSMS. So setup SSMS on a computer and ensure you can access the server with the username and password to that database server.
If you don't get the above 2 right, you are simply wasting your time as you cant access the database. This can either be because the user you setup is wrong, doesn't have remote access enabled (if needed), or the ports are not opened (if needed), among many other reasons but these being the most common.
Once you have verified that you can access the database using SSMS. The next step, just for the sake of automating the process and avoiding mistakes, is to let the system do the work for you.
Start up an empty project, add your choice of Linq to SQL or Dataset (EF is good but the connection string is embedded inside of an EF con string, I want a clean one), and connect to your database using the details verified above in the con string wizzard. Add any table and save the file.
Now go into the web config, and magically, you will see nice clean working connection string there with all the details you need.
{ Below was part of an old post so you can ignore this, I leave it in for reference as its the most basic way to access the database from only code behind. Please scroll down and continue from step 2 below. }
Lets assume the above steps start you off with something like the following as your connection string in the code behind:
string conString = "Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;";
This step is very important. Make sure you have the above format of connection string working before taking the following steps. Make sure you actually can access your data using some form of sql command text which displays some data from a table in labels or text boses or whatever, as this is the simplest way to do a connection string.
Once you are sure the above style works its now time to take the next steps:
1.
Export your string literal (the stuff in the quotes, including the quotes) to the following section of the web.config file (for multiple connection strings, just do multiple lines:
<configuration>
<connectionStrings>
<add name="conString" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
<add name="conString2" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
<add name="conString3" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
{ The above was part of an old post, after doing the top 3 steps this whole process will be done for you, so you can ignore it. I just leave it here for my own reference. }
2.
Now add the following line of code to the C# code behind, prefrably just under the class definition (i.e. not inside a method). This points to the root folder of your project. Essentially it is the project name. This is usually the location of the web.config file (in this case my project is called MyProject.
static Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/MyProject");
3.
Now add the following line of code to the C# code behind. This sets up a string constant to which you can refer in many places throughout your code should you need a conString in different methods.
const string CONSTRINGNAME = "conString";
4.
Next add the following line of code to the C# code behind. This gets the connection string from the web.config file with the name conString (from the constant above)
ConnectionStringSettings conString = rootWebConfig.ConnectionStrings.ConnectionStrings[CONSTRINGNAME];
5.
Finally, where you origionally would have had something similar to this line of code:
SqlConnection con = new SqlConnection(conString)
you will replace it with this line of code:
SqlConnection con = new SqlConnection(conString.ConnectionString)
After doing these 5 steps your code should work as it did before. Hense the reason you test the constring first in its origional format so you know if it is a problem with the connection string or if it is a problem with the code.
I am new to C#, ASP.Net and Sql Server. So I am sure there must be a better way to do this code.
I also would appreicate feedback on how to improve these steps if possible. I have looked all over for something like this but I eventually figured it out after many weeks of hard work. Looking at it myself, I still think, there must be an easier way.
I hope this is helpful.
it should be within the <configuration> node:
<connectionStrings >
<add name="myconnectionstring" connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
</connectionStrings>
this site has more info on it:
Connection in WebConfig
Add the your connection string to the <connectionStrings> element in the Web.config file.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com" providerName="System.Data.SqlClient" />
</connectionStrings>
In Class.Cs
public static string ConnectionString{
get{
return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;}
set{}
in header
using System.Configuration;
in code
SqlConnection conn = new SqlConnection(*ConfigurationManager.ConnectionStrings["connstrname"].ConnectionString*);
You can also use external configuration file to specify connection strings section, and refer that file in application configuration file like in web.config
Like the in web.config file:
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
The external configuration connections.config file will contains connections section
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
Modifying contents of external configuration file will not restart the application (as ASP.net does by default with any change in application configuration files)
If you want to write connection string in Web.config then write under given sting
<connectionStrings>
<add name="Conn" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com"
providerName="System.Data.SqlClient" />
</connectionStrings>
OR
you right in aspx.cs file like
SqlConnection conn = new SqlConnection("Data Source=12.16.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com");
You can put this in your web.config file connectionStrings:
<add name="myConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
You can use following format:
<connectionStrings>
<add name="ConStringBDName" connectionString="Data Source=serverpath;Initial Catalog=YourDataBaseName;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
Most probably you will fing connectionstring tag in web.config after <appSettings>
Try out this.
You can try this. It is very simple
<connectionStrings>
<add name="conString" connectionString="Data Source=SQLServerAddress;Initial Catalog=YourDatabaseName; User Id=SQLServerLoginId; Password=SQLServerPassword"/>
</connectionStrings>
Try this for your connection string.
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;
I JUST FOUND!!
You need to put this string connection and point directly to your database.
Same case on server.
"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=c:/inetpub/wwwroot/TEST/data/data.mdb;"
It works!! :)
Store connection string in web.config
It is a good practice to store the connection string for your application in a config file rather than as a hard coded string in your code. The way to do this differs between .NET 2.0 and .NET 3.5 (and above). This article cover both.
https://www.connectionstrings.com/store-connection-string-in-webconfig/
Create a section called <connectionStrings></connectionStrings> in your web.config inside <configuration></configuration> then add different connection strings to it, for example
<configuration>
<connectionStrings>
<add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>
</connectionStrings>
</configuration>
Here's a list of all the different connection string formats https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx

ASP.NET EFCodeFirst not using correct connection string

I am trying to publish a website using ASP.NET MVC3 EF and CODEFIRST with a SQL Server 2008 backend. On my local machine I was using a sql express db for development, but now that I am pushing live, I want to use my hosted production database. The problem is that when I try to run the application, it is still using my local db connection string. I have completely removed the old connection string from my web.config file and am using the <clear /> tag before creating the new connection string. I have also cleaned the solution and rebuilt, but somehow it is still connecting to the old db. What am I missing?
This is the new connection string:
<connectionStrings>
<clear />
<add name="CellularAutomataDBContext"
connectionString=" Server=XXX;
Database=CellularAutomata; User ID=XXX; Password=XXX; Trusted_Connection=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
UPDATE
When I debug and look at the DBCONTEXT object, this is what is showing up for its connection:
Data Source=.\\SQLEXPRESS;Initial Catalog=CellularAutomata.Models.D1K2N3CARuleDBContext;Integrated Security=True;MultipleActiveResultSets=True"
I am unsure why this is happening because I cannot find it being set to this anywhere. Also, under configuration it says LazyLoadingEnabled = true, I assume this may be part of the problem, maybe it is not loading the new connection string. Where do I change these parameters?
UPDATE 2
EFCodeFirst is using a default connection string, I can't figure out how to get it to accept the connection string that I specify in the web.config file.
So, When using EF CodeFirst, there is a default connection string that it uses. If you want to be able to use a custom connection string, there are a few parameters guidelines that you must follow.
name ="this must match the name of your database context class"
connectionString="Server=yourserverurl; Database=yourdatabasename; User ID=youruserid;
Password=yourpassword; Initial Catalog=the name of the database to use;
Trusted_Connection=False"
providerName="System.Data.SqlClient"
So far this is working for me.
The connectionString you show is not an EF connection string. The EF won't use it. So you're changing the wrong thing.
An EF connectionString will include providerName="System.Data.EntityClient"
It will look for the same name as your context and depending on what else you
are using other names as well. I usually use the following for controlling
specific features with either the same or specific connection strings
(I keep app services in a different db for example so EFCF can drop tables as needed):
<connectionStrings>
<add name="MyAppContext" .../>
<add name="ApplicationServices" .../>
<add name="DefaultConnection" .../>
</connectionStrings>

Resources