问题
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