Linq-to-SQL database file empty

2019-08-23 12:55发布

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条回答
你好瞎i
2楼-- · 2019-08-23 13:42

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.

查看更多
登录 后发表回答