Ich habe zwei Instanzen von SQL Server - SQLEXPRESS
und MSSQLServer 2017
. Meine Entity Framework-Testanwendung erstellt die Datenbank in SQLEXPRESS
. Wo wird definiert, welche Instanz von SQL Server Entity Framework verwendet werden soll?Standard SQL Server für Entity Framework
Test-Anwendung:
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: Ich habe mehrere Verbindungszeichenfolgen in meinem 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>
Wie EF entscheidet, welche zu benutzen?
Ich habe mehrere Verbindungszeichenfolgen. Wie entscheidet EF, welches benutzt werden soll? Weitere Details in UPD. – vico
@Vico: Wenn SQL Express mit nicht konkretem DbContext gewählt wurde, wird wahrscheinlich SQL Express als Standard gewählt. MSDN: "Wenn Sie keine Verbindungszeichenfolge angeben, erstellt Entity Framework im Verzeichnis users eine LocalDB-Datenbank mit dem vollständig qualifizierten Namen der DbContext-Klasse". Siehe Ende von https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string. –
@HugoUchoBruno Der Benutzer hat den Kommentar gelöscht. –