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?