5

I have installed EF Version 6.1.3 on all my projects. I then created a model class, a context class, and installed a MSSQL database on my local computer (I did not have done anything else). Everything worked just perfectly (somehow it knew about my local database).

Model class:

public class Account
    {
        public int Id { get; set; }
        public string Name{ get; set; }
    }

DataContext class:

public class MyClassDataContext: DbContext
    {
        public DbSet<Account> Accounts{ get; set; }
    }

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

I then tried to move it to a remote database and it doesn't work. I tried everything.

What is the right approach, to get the job done?

EDIT: I tried this connection string and nothing happens. The app still tries to connect with the local database.

<connectionStrings>
    <add name="MyClassDataContext" connectionString="Data Source=MyRemoteServer;Initial Catalog=MyRemoteCatalog;Integrated Security=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <configSections>
2
  • Here is an example link . But it doesn't work for me. I don't have those metadata. But even if I remove them, I still get error messages... Commented Nov 17, 2015 at 16:33
  • i cant see the connection string, when adding ef best practice to add connection string to config file Commented Nov 17, 2015 at 16:36

1 Answer 1

3

You need to create either make sure that your connections string's name is the fully qualified name of your context, or create an explicit default constructor for your context. Since you mentioned it in the comments, the link you provided isn't working for you because you're using code-first. Try this link instead.

Below is a fully functional console app that can demonstrate how it should work, along with the config file. This will use our local SQLServer installation, but not the SQLExpress. It should work fine for any remote database as well.

Note that in the previous app config that I had posted, I put the connection string section at the top. That is incorrect: configSections must be the first node.

namespace TestApp
{
    public class Account
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class MyClassDataContext : DbContext
    {
        public DbSet<Account> Accounts { get; set; }
    }

    class Program
    {    
        static void Main(string[] args)
        {
            using (var x = new MyClassDataContext())
            {
                x.Accounts.Add(new Account { Name = "Drew" });
                x.SaveChanges();

                var y = x.Accounts;
                foreach (var s in y)
                {
                    Console.WriteLine(s.Name);
                }
            }

            Console.ReadKey();
        }
    }
}

The configuration file:

<configuration> 
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="ConsoleApplication4.MyClassDataContext" connectionString="Data Source=.;Initial Catalog=MyClass;Integrated Security=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>  
</configuration>
Sign up to request clarification or add additional context in comments.

4 Comments

It is only necessary to override the constructor if you want your connection string to be different from the EF conventions. EF uses conventions to look for connection strings, so it's looking for a connection string named <fullyqualifiednamespace>.DivAppsDataContext . Which it isn't finding, so it's falling back to the default LocalSqlServer string in machine.config.
I tried with the constructor and also with the naming convention way. What I am missing here?
@ErikFunkenbusch thanks for the reminder, I made an edit to say as much. arnie, I posted a test console app that successfully connects, give it a try and see if it works for you.
@DrewJordan It works. You're right, I have code first approach. Thank you a lot.

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.