从 VS 中启动应用程序时删除 MDF 表内容 [英] MDF table content is deleted when starting application from within VS

查看:39
本文介绍了从 VS 中启动应用程序时删除 MDF 表内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里非常神秘的问题.

我在项目中有一个单表的 mdf 文件(双击将显示这个):

I have mdf-file in project with single table (double-clicking will show this):

CREATE TABLE [dbo].[Table] (
    [Id]      DATETIME        NOT NULL,
    [Version] NCHAR (20)      NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

然后我添加 dbml (Linq-to-SQL),将表拖到那里,添加一些代码

Then I add dbml (Linq-to-SQL), drag table there, add some code

partial class TestDataContext
{
    public static string DatabaseFile { get; private set; }
    public static string ConnectionString { get; private set; }

    static TestDataContext()
    {
        DatabaseFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.mdf");
        ConnectionString = string.Format(@"Data Source=(LocalDB)\v11.0;AttachDbFilename={0};Integrated Security=True", DatabaseFile);
    }

    public TestDataContext Attach()
    {
        Detach();
        var context = new TestDataContext(ConnectionString);
        // re-create database
        if (!context.DatabaseExists() && !File.Exists(DatabaseFile))
            context.CreateDatabase();
        return context;
    }

    public static void Detach()
    {
        try
        {
            using (var master = new DataContext(@"Data Source=(LocalDB)\v11.0;Initial Catalog=master;Integrated Security=True"))
            {
                master.ExecuteCommand(string.Format(@"ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE", DatabaseFile));
                master.ExecuteCommand(string.Format(@"exec sp_detach_db '{0}'", DatabaseFile));
            }
        }
        catch { } // should be safe
    }

    public void Test()
    {
        var row = new Table() { Id = DateTime.Now, Version = "test1.0" };
        Tables.InsertOnSubmit(row);
        System.Threading.Thread.Sleep(5);
        row = new Table() { Id = DateTime.Now, Version = "test1.1" };
        Tables.InsertOnSubmit(row);
        SubmitChanges();
    }
}

如果文件被删除,则需要分离数据库并重新创建数据库(可能与问题有关,请参阅进一步内容).该数据库是在exe文件附近创建的(不要与项目中的数据库混合使用,仅用于定义结构和生成dmbl).

It's all needed to detach database and to re-create database if file is deleted (maybe it's related to issue, see further). That new database is created near exe-file (don't mix it with one in project, used only to define structure and generate dmbl).

现在的问题是:当我从 VS (F5) 中运行应用程序时,该表总是空白.甚至在调用 Attach() 之前,它上次运行的内容都被删除了!看起来如果 VS 打开 new mdf 文件,在那里找到我的表(笑)并清除它!

Now the problem: when I run application from within VS (F5), then this table is always blank. Even before calling Attach() its content from previous run is erased ! It looks like if VS opens new mdf-file, find my table there (lol) and clears it!

最令人困惑的部分:如果编译的 exe 文件直接运行,则内容在启动之间保持不变(按预期).一旦我在 VS 中点击 F5 .. 它又变空了.

Most confusing part: if compiled exe-file is running directly, then content persist between launches (as intended). As soon as I hit F5 in VS .. it become empty again.

有什么想法吗?

我在 VS数据库资源管理器"中打开 mdf 文件,以便能够看到发生了什么.应用程序通过调用 Test() 添加一些行,我在应用程序和资源管理器中看到了这一点.然后应用程序退出.数据还在.是的,我点击刷新按钮,它仍然.然后我点击 F5 并在按下调用 Attach() 的按钮之前切换到资源管理器并在那里点击刷新......表是空的.

I open that new mdf-file in VS "Database Explorer", to be able to see what is going on. Application adds some rows by calling Test() and I see that in application and in explorer. Then application exits. Data still. Yes, I click refresh button, it is still. Then I hit F5 and before pressing button which call Attach() switch to explorer and hit refresh there.. table is empty.

推荐答案

您的解决方案中有 MDF 文件吗?检查复制到输出目录"的文件属性.它应该在请勿复制"

Is the MDF file in your solution? Check the file properties for the 'Copy to Output Directory'. It should be on "Do not copy"

这篇关于从 VS 中启动应用程序时删除 MDF 表内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆