0

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);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

use this connectionString .

attachdbfilename must be relative. When running, the program copies a copy of the .mdb file to the bin folder.

data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Musik.mdf;integrated security=True;

Enter your .mdb file name in attachdbfilename=|DataDirectory|\Musik.mdf

pay attention |DataDirectory|\your mdf File name

Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
0

Do NOT use AttachDbFileName, it has been deprecated. It creates a copy of the database which is deleted afterwards, and holds open the file so that when you try to open it again in another connection then it fails.

Instead, attach the database normally, using CREATE DATABASE... FOR ATTACH.

See Bad habits: Using AttachDBFileName by Aaron Bertrand, also this Stackoverflow question.

Charlieface
  • 52,284
  • 6
  • 19
  • 43