0

I have two instances of SQL Server - SQLEXPRESS and MSSQLServer 2017. My Entity Framework test application creates database in SQLEXPRESS. Where is it defined which instance of SQL Server Entity Framework should use?

Test application:

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

    public class PeopleContext : DbContext
    {
        public IDbSet<Person> People { get; set; }
    }

    static void Main(string[] args) // using DB
    {
        try
        {
            using (PeopleContext ctx = new PeopleContext())
            {
                ctx.People.Add(new Person() { Id = 1, Name = "John Doe" });
                ctx.SaveChanges();
            }

            using (PeopleContext ctx = new PeopleContext())
            {
                Person person = ctx.People.SingleOrDefault(p => p.Id == 1);
                Console.WriteLine(person.Name);
            }

        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message);
        }

        Console.WriteLine("finish");
        Console.ReadLine();
    }

UPD: I have several connection strings in my 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=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="LearnCSharpConn_server" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=LearnCSharp;Integrated Security=True" />
    <add name="LearnCSharpConn" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LearnCSharp.mdf;Integrated Security=True" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <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>

How EF decides which one to use?

3
  • I have several of connection strings. How EF decides which one to use? More details in UPD. Commented Nov 15, 2017 at 14:05
  • @Vico: If it has chosen SQL Express with non concrete DbContext it is probably chooses SQL Express as default. MSDN, “If you don't specify a connection string, Entity Framework will create a LocalDB database in the users directory with the fully qualified name of the DbContext class”. See end of learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/…. Commented Nov 15, 2017 at 15:01
  • @HugoUchoBruno Nevermind, the user deleted the comment. Commented Nov 15, 2017 at 15:02

2 Answers 2

1

You could use this:

public class PeopleContext : DbContext
{
    public PeopleContext() : base("Database") //it makes reference to the connection string defined in the app.config
    {     
    }
    public IDbSet<Person> People { get; set; }
}

And in your app.config:

<connectionStrings>
<add name="Database" connectionString="Data Source=source;Initial Catalog=Your_database;Integrated Security=True" providerName="System.Data.SqlClient" />

Sign up to request clarification or add additional context in comments.

Comments

0

To discover the server, username, password and default database you should seek to find the connection string being used by Entity Framework.

You do not specify which version of Entity framework you use, however most versions store the connection string in either web.config or app.config.

Alternatively, you may step into the PeopleContext constructor via Visual Studio's debug mode to view a connection string, or in .Net Core the connection string can be found by stepping through the OnConfigure method in PeopleContext.cs.

Failing that you may Ctrl + f to search connectionString over entire solution.

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.