Linq-to-SQL数据库文件为空 [英] Linq-to-SQL database file empty

查看:80
本文介绍了Linq-to-SQL数据库文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是stackoverflow的新手,我已经搜索了整个站点和msdn,但一无所获.我希望你能帮助我!

I'm new to stackoverflow an I've searched the whole site and the msdn and found nothing. I hope you can help me!

我想在.sdf文件中包含一个Linq-to-SQL数据库.因此,我在ORM设计器中为Linq-to-SQL设计了数据库sheme,并编写了以下代码:

I want to have a Linq-to-SQL database in an .sdf file. So I designed the database sheme in the ORM designer for Linq-to-SQL and wrote the following code:

DataShemeDataContext context = new DataShemeDataContext("Data Source=database.sdf");
if (!context.DatabaseExists())
  context.CreateDatabase();
Console.ReadLine();

我的问题如下:

  • 当我运行程序并按 Enter 键退出程序时,一切都很好
  • 但是,如果我单击控制台窗口顶部的"X"按钮,则数据库文件的大小仅为20kb(而不是84kb),并且为空.
  • when I run the program and exit it by pressing the Enter key, everything is fine
  • But if I click on the "X" button in the top of the console window, the database file is only 20kb size (instead of 84kb) and it is empty.

使用此行:

context.SubmitChanges();

没有区别.

希望你能帮助我!

推荐答案

使用环绕您的代码:

using(DataShemeDataContext context = new DataShemeDataContext("Data Source=database.sdf"))
{
    if (!context.DatabaseExists())
        context.CreateDatabase();
}
Console.ReadLine();

通过这种方式,您可以确保正确处理上下文.

This way you are sure that your context is properly disposed.

这篇关于Linq-to-SQL数据库文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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