I don't know what's wrong with this code , this code compile successfully but doesn't update database . do i miss some thing ?
DataClassesDataContext 开发者_运维技巧db = new DataClassesDataContext();
var query = from p in db.AllPatiences
select p;
int newID = 1001;
foreach (AllPatience patient in query)
{
patient.Id = newID.ToString();
newID++;
}
db.SubmitChanges();
My guess is that you're working with a file-based database (.mdf) in your project, and you're trying to look at the table data in that database. When you build your project, the database is cloned into the bin/debug/ or bin/release/ directory, where your running program accesses it. If you take a look at this version of the file, rather than the one you have loaded into your VS project, you should see the changes.
If this is the case, you should set the database file property to "Copy only if newer" or "Do not copy", to avoid it cloning the DB each build.
is patient.Id generated in the database itself? Possibly as an SQL IDENTITY field? Also, check the SyncOn property of the field in your dbml file.
I found the problem , For Update like that , It's necessary to have primary key . The reason my table didn't have primary key was : I imported those data from an Excel file , So i didn't have Primary key .
Guess, is "Id" your tables primary key? If so, I don't think that L2S supports an update of the primary key (what's very correct in my opinion). You should never attend to change the value of the primary key.
If Id is not your primary key, I have no clue :-P.
精彩评论