Ich habe einen Kodex zunächst mit dem folgenden DbContext einrichtenEntity Framework Connection String „Der Server wurde nicht gefunden“
public class MyDatabaseDbContext : DbContext
{
public MyDatabaseDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
public MyDatabaseDbContext()
: base("MyDatabase")
{
}
public DbSet<MyTable> MyTables { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
Ich habe auch die Verbindungszeichenfolge betrachten die (lokale) Datenbank
einrichten<connectionStrings>
<add name="MyDatabase"
connectionString="Server=(local);Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
auch die initializer in Global.asax
Database.SetInitializer<MyDatabaseDbContext>(new DropCreateDatabaseAlways<MyDatabaseDbContext>());
einrichten ich die Anwendung ein, dann laufen nd die DbContext nennen (in meinem Fall von der Steuerung)
var dbContext = new MyDatabaseDbContext();
var myTableResults = dbContext.MyTables.ToList();
Ich erhalte die folgende Fehlermeldung
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
Auf Debuggen und Blick auf die DbContext Variable> Datenbank> Verbindung> Connectionstring Property sehe ich die folgende: "Data Source = (localdb) \ v12.0; AttachDbFilename = | Datadirectory | MyDatabase.mdf; Initial Catalog = MyDatabase; Integrated Security = True; Multiple = True"
Fragen
Warum zeigt es auf die Datenbank (localDb) \ v12.0 und nicht auf meine Verbindungszeichenfolge? Trotzdem, warum erstellt es die Datenbank dort sowieso nicht, da dies auf meinem Dev-Rechner ist?
Gibt es eine Art Konvention, die ich vergessen habe, die ich einstellen muss.
versuchen Sie, Ihre Db-Datei umzubenennen. – rashfmnb