This error is thrown:
Microsoft.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file C:...\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'
when I want to count the Artists in my Music db to set the titel
public partial class MainWindow : Window
{
private readonly MusicContext db;
private readonly IServiceProvider serviceProvider;
public MainWindow(MusicContext db, IServiceProvider serviceProvider)
{
InitializeComponent();
this.db = db;
this.serviceProvider = serviceProvider;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Title = (db.Artists.Count()).ToString();
}
}
class Startup
{
public void ConfigureServices(IConfiguration configuration, IServiceCollection services)
{
string connectionString= "Server = (LocalDB)\\mssqllocaldb; attachdbfilename = C:....Musik.mdf; integrated security = True";
services.AddDbContext<MusicContext>(x => x.UseSqlServer(connectionString));
services.AddSingleton<MainWindow>();
}
}
I already change the App.xaml.cs so that it will start my Startup. About an hour ago it worked fine but now it won't start without throwing an error, i hope you could help me.
public partial class App : Application
{
private readonly IHost host;
public App()
{
var startup = new Startup();
host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
startup.ConfigureServices(context.Configuration, services);
}).Build();
}
protected override async void OnStartup(StartupEventArgs e)
{
await host.StartAsync();
var mainWindow = host.Services.GetRequiredService<MainWindow>();
mainWindow.Show();
base.OnStartup(e);
}
protected override async void OnExit(ExitEventArgs e)
{
using (host)
{
await host.StopAsync(TimeSpan.FromSeconds(5));
}
base.OnExit(e);
}
}