1

I'm building a MVC 3 application and use Entity Framework 4.3 code-first. My context is becoming complex so I've decided to working with SQL Server 2008 R2. Then I changed my connection string in web.config. I checked and I know the new connection string is correct, but EF is still working with the default connection string (.\SQLEXPRESS ...) from SQL Server CE (I have now deleted SQL Server CE from nuget packages).

My context name is CodeFirstContext and my connection string name is CodeFirstContext, too. But the return context.Products; always fails and/because the connection string in context says SQLEXPRESS (my web.config sets the connection string to SQLSERVER).

What is going on here? I erased all default connection strings in the project. When I am running the project, it still gets all data from SQL Server Express.

I have been searching for about two days. Why is this so difficult if so many people only have to change the connection string and then it works.

4 Answers 4

1

You might be picking up the connection string from your root/machine config, in your web.config file add a clear element like so

<connectionStrings>
  <clear/>
  .. your connection strings here
...
Sign up to request clarification or add additional context in comments.

1 Comment

same connectionstring is working fine on another test application(it's not code-first)
1

I 've solved the problem like that:

public class CodeFirstContext : DbContext
{
    public CodeFirstContext() : base("RandevuIzle")
    {
    }
    public DbSet<User> Users { get; set; }
}


<connectionStrings>
  <add name="RandevuIzle" connectionString="Data Source=111.222.333.444\MSSQLSERVER2008; Initial Catalog=RandevuIzle;User Id=MyUserName;Password=myPassword" providerName="System.Data.SqlClient" />

Somethings went wrong but i cannot understand. I re-start the project after doing this code below. Then it's work normally.

Comments

0

As you instantiate your DbContext, are you providing a name that isn't CodeFirstContext?

Comments

0

Holy SQL Smoke, Batman! This drove me totally batty today. Getting EF 5.0 up and running w/ VS2010 on a new project was much more difficult than I imagined. I had the same problem and found this great Ode to the Code ::

http://odetocode.com/blogs/scott/archive/2012/08/15/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx

And to be specific ::

 public class DepartmentDb : DbContext 
 {
    public DepartmentDb()
        : base(@"data source=.; 
                 initial catalog=departments;
                 integrated security=true")
    { }

    public DbSet<Person> People { get; set; }
}

Got me the result I was needing. Do I think it's cool that I've got to set the data source like this and that it won't find the obvious solution within the app.config? No, I don't. But I have officially spent too much time attempting a transition from LINQ to SQL over to EF today and am going to go ahead and get some of the application done while I still have some interest in doing so.

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.