Linq-to-SQL database file empty

女生的网名这么多〃 提交于 2019-12-11 19:29:58

问题


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

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();

My problem is the following:

  • 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.

Using this line:

context.SubmitChanges();

makes no difference.

I hope you can help me!


回答1:


Surround your code with an using :

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.



来源:https://stackoverflow.com/questions/12002802/linq-to-sql-database-file-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!