簡體   English   中英

“數據庫已經存在。 選擇一個不同的數據庫名稱。 恢復C#Win App數據庫后,無法附加文件…”錯誤

[英]“Database already exists. Choose a different database name. Cannot attach the file…” error after restoring database of c# win app

我開發了一個使用數據庫“ OlgooDB.mdf”的C#win應用程序。

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<OlgooContext, Configuration>());

應用程序->遷移->配置:

internal sealed class Configuration : DbMigrationsConfiguration<App.Model.OlgooContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
        ContextKey = "App.Model.OlgooContext";

        string dataDirPath = Application.StartupPath + "\\Database";
        AppDomain.CurrentDomain.SetData("DataDirectory", dataDirPath);
    }

    protected override void Seed(App.Model.OlgooContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

當連接字符串定義為:

<connectionStrings>
<clear/>
<add name="OlgooContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog = OlgooDB; Integrated Security=True; " providerName="System.Data.SqlClient"/> </connectionStrings>

我可以毫無問題地還原數據庫,但是當我將連接字符串更改為:

 <connectionStrings>
<clear/>
<add name="OlgooContext" connectionString="Data Source =.\SQLExpress;AttachDbFilename=|DataDirectory|\OlgooDB.mdf; Database=OlgooDB; Integrated Security=True;" providerName="System.Data.SqlClient"/> </connectionStrings>

恢復后發生此錯誤:

system.data.sqlclient.sqlexception(0x80131904):數據庫'c:\\ program files \\ microsoft sql server \\ mssql10.sqlexpress \\ mssql \\ data \\ OlgooDB.mdf'已經存在。 選擇一個不同的數據庫名稱。 無法將文件'D:... \\ bin \\ Debug \\ Database \\ OlgooDB.mdf'作為數據庫'OlgooDB'附加...

恢復功能:

private void backgroundWorkerRestore_DoWork(object sender, DoWorkEventArgs e)
    {
        using (var context = new OlgooContext())
        {
            string command = "ALTER DATABASE OlgooDB SET OFFLINE WITH ROLLBACK IMMEDIATE " +
                                                    " RESTORE DATABASE OlgooDB FROM DISK='" + txtRestorePath.Text + "'WITH REPLACE " +
                                                    "ALTER DATABASE OlgooDB SET ONLINE";
            context.Database.CommandTimeout = 360;
            context.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, command);
        }
    }

有人知道如何解決此錯誤嗎?

提前致謝。

您對連接Express instancelocaldb感到困惑。

您的第一個連接字符串可以連接到Express實例OlgooDB數據庫: connectionString="Data Source=.\\SQLEXPRESS; Initial Catalog = OlgooDB; Integrated Security=True; " providerName="System.Data.SqlClient"

在這種情況下,數據庫已被附加,無需再附加一次,它不是RESTORE錯誤的錯誤,而是第二個連接字符串:

"Data Source =.\SQLExpress;AttachDbFilename=|DataDirectory|\OlgooDB.mdf; Database=OlgooDB; Integrated Security=True;" providerName="System.Data.SqlClient"

在這里,您使用語法連接到localdb,其中未附加.mdf,並且在連接到localdb時將其附加

當然,當您嘗試為連接創建數據庫時,它會失敗,因為數據庫已經存在。

因此,您的連接字符串N2是錯誤的:如果使用localdb,則在使用.mdf之前附加它。 但是,您使用的是永久連接的數據庫,而無需附加數據庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM