问题
I am using LINQ to SQL with Sql Server Compact Edition 3.5 and VS2008.
I have a very simple table (Tokens) with a uniqueidentifier primary key (TokenID) and two other nullable fields (UsedBy and UsedOn). I am trying to use LINQ to insert new rows into the table but for some reason they are not persisting.
Here is my code:
var connectionstring = "Data Source=|DataDirectory|\\MyData.sdf";
MyData db = new MyData(connectionstring) { Log = Console.Out };
Tokens token = new Tokens { TokenID = Guid.NewGuid() };
db.Tokens.InsertOnSubmit(token);
//db.GetChangeSet();
db.SubmitChanges();
var tokens = from t in db.Tokens
select t;
foreach (var t in tokens)
{
Debug.Print(t.TokenID.ToString());
}
If I uncomment db.GetChangeSet(); I can see the pending insert and when I iterate and print them to the debug widow the # of tokens grows each time. But if I query the table in VS (via Show Table Data) it is empty? Viewing the data like this also "resets" the tokens returned by LINQ their original state.
I am pretty sure I am making some simple mistake, but I can't see it. Any ideas?
回答1:
Check that your DB file is not copied to the output directory at each build (in the property page of the project item)
回答2:
Of course 5 minutes after posting my question I have a huge DUH! moment. Turns out I was refreshing the database in my project to see if the inserts were persisting when I should have been checking \bin\Debug\MyData.sdf instead.
Trust the code Sean, trust the code. Oh, and remember you're an idiot sometimes.
来源:https://stackoverflow.com/questions/1343816/why-are-my-linq-inserts-not-persisting-in-sql-server-ce-3-5