0

i have changed the connection string to point to a database in the remote server. But when I execute the project the program still points to the local db.

<entityFramework>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>


<connectionStrings> 
<add name="TEDALS_Ver01.DAL.TedalsContext" 
  connectionString="Data Source=FE0VMC0643\SQLEXPRESS;
  AttachDbFilename=|DataDirectory|\TeDaLSdev.mdf;
  Integrated Security=False" 
  providerName="System.Data.SqlClient" 
/>
<!--<add
   name="TEDALS_Ver01.DAL.TedalsContext"
   connectionString="Server=FE0VMC0643; Database=TeDaLSdev; "
   providerName="System.Data.SqlClient" />-->

<!--<add name="TEDALS_Ver01.DAL.TedalsContext"
     providerName="System.Data.SqlClient" 
     connectionString="Server=FE0VMC0643;Data Source=FE0VMC0643;
     Initial Catalog=TeDaLSdev;
     Integrated Security=True;
     Connect Timeout=15;
     Encrypt=False;
     TrustServerCertificate=False;" />-->

</connectionStrings> 

Comments are the different connection strings i have treid so far. i never had a connections string when I was using the LocalDB.

Constructor for Connection

public class TedalsContext : DbContext
{
    public TedalsContext()
        : base("TedalsContext")
    {
        //Database.SetInitializer<TedalsContext>(null);
    }
}

i am using SQL Server Express as my database. I have also tried changing the name of the parameter for base in constructor as the name of the Database. But it did not change anything.

I have already tried if I have access to the database through SSMS. I am able to create tables but I am unable to rename the database as such(I do not have access rights to rename the database TeDalSdev).

Are there any other work around i could try? Should the name of the remote database and the local database should be the same to avoid changing a lot of code?

UPDATE

Controller

public class LsystemFamiliesController : Controller
{
    private TedalsContext db = new TedalsContext();
    //Code goes here
}
Vini
  • 1,978
  • 8
  • 40
  • 82
  • call base with base("TEDALS_Ver01.DAL.TedalsContext") ? In the documentation [here](https://msdn.microsoft.com/en-us/data/jj556606.aspx#Connection) it says EF will get the connection string differently for code first and designer generated contexts.. – Oguz Ozgul Nov 30 '15 at 08:01
  • But independently, the name you pass to the DBContext constructor whould be the name of the connection string. You are passing TedalsContext only, which does not exist in your configuration file as it seems. – Oguz Ozgul Nov 30 '15 at 08:03
  • See [this page](https://msdn.microsoft.com/en-us/data/jj592674) too – Oguz Ozgul Nov 30 '15 at 08:04
  • @OguzOzgul: Now i have added a new db in the server with exactly the same name as my localdb. i really do not understand the different parameters int he connection string. So i have to go with the trial and error to get it somehow working. – Vini Nov 30 '15 at 08:06
  • db name is not the issue here don't you see? The name of the connection string "TedalsContext" does not match to any connection string in the configuration file. Try base("TEDALS_Ver01.DAL.TedalsContext") ? – Oguz Ozgul Nov 30 '15 at 08:08
  • Added that. Changed the connection string to exactly what is there in the properties of the database. Still it accesses the local db. – Vini Nov 30 '15 at 08:11
  • Can you show me the first part of your program that accesses the database? Can you also check that all DB access is done through Entity Framework context? It may be that you have some low level access such as using a SqlConnection directly. – Paul Nov 30 '15 at 08:53
  • i have no places where a connection to the database is established in the program. All the controllers access the data via the entity framework context. But i will add first part of code in a controller – Vini Nov 30 '15 at 09:00
  • Why is the question being downvoted? – Vini Nov 30 '15 at 14:09

2 Answers2

4

I've added a connection string from a web config file of one of my projects and all the fields that (should) be checked. This is within the <connectionStrings> tags. You need to make it your default connection and you can only have one connection string as the default connection.

<add name="DefaultConnection" providerName="System.Data.SqlClient" 
    connectionString="Data Source=Providedbyhost;integrated 
    security=false;Initial Catalog=DB_Name;User Id=UserId;Password=password;
    Trusted_Connection=false; TrustServerCertificate=true;
    Encrypt=true;MultipleActiveResultSets=True" />

Where you have your DB Context:

public TedalsContext()
    : base("DefaultConnection")

Switch everything to DefaultConnection, until you get it working, then you can focus on changing names. Comment out your local db connection strings. Even remove it from the project if you need to (and save it elsewhere), while you are testing this.

I've also added a screen shot of the my folder directory, to show which web config folder I am modifying. See it's highlighted in blue.

enter image description here

Also this screen shot shows you where to navigate to test your connection string within VS.

enter image description here

I suggest you look here:

How to: Access SQL Server Using Windows Integrated Security

What will be the connectionstring for mssql windows authentication

and this:

Connection string using Windows Authentication

As I suggested in the discussion. I recommend also contacting web hosting provider, as I have had this problem where it will not connect and it's been a problem at the hosting end.

If you need more help, shout out.

Community
  • 1
  • 1
  • i am using windows authentication. So i can skip the username and password fields?? – Vini Nov 30 '15 at 08:18
  • And i have the connection active in my server explorer. But still the data that is being fetched from the local db. – Vini Nov 30 '15 at 08:20
  • so now i have changed the connection exactly as yours: with details about my server and db;removed username and password, still no changes. is there something that i will need to change in the entity framework section? – Vini Nov 30 '15 at 08:24
  • Yes. I have done that as well. No changes. I am editing the config file which is in the root directory. And thanks for helping. – Vini Nov 30 '15 at 08:27
  • I am also modifying the same config file. yes it annoying because i have been trying this since thursday. and havent found anything. – Vini Nov 30 '15 at 08:31
  • I have also disconnected the local server. And in the server explorer now i do not find the connected server. – Vini Nov 30 '15 at 08:58
  • i only find in the server default connection and in the properties the name of the server is `TedalsContext`. is it the expected behaviour? – Vini Nov 30 '15 at 10:43
  • 1
    Your answer led me to the right direction. With an additional line in my controller it worked perfectly. i had to add `base("name=TedalsContext")`. Now it is working.. thanks a lot.. reallyy a lot.. :) :) :) – Vini Nov 30 '15 at 14:11
  • 1
    No. With default connection laso it will work. But i have to add the attribute `name` in base. But your links really helped a lot.. And i could understand that the database was loaded with ur screenshots.. – Vini Nov 30 '15 at 14:47
0

Under your TedalsContext constructor, use : base(connectionStringName).

Otherwise, the context will treat it as code first and create a TedalContext inside your SQLExpress Local DB.

public class TedalsContext : DbContext
{
    public TedalsContext()
        : base("TEDALS_Ver01.DAL.TedalsContext")
    {
        //Database.SetInitializer<TedalsContext>(null);
    }
}