linq to sql (VS 2010) - insert doesn't work
I have a class containing my data context:
private static drDataContext db = new drDataContext();
I also have the method:
public static void InsertSelfRatings(List<DeveloperSelfRating> ratings)
{
foreach (DeveloperSelfRating sr in ratings)
{
db.DeveloperSelfRatings.InsertOnSubmit(sr)开发者_运维百科;
}
// db.DeveloperSelfRatings.InsertAllOnSubmit(ratings);
db.SubmitChanges();
}
I tried both InsertOnSubmit() and InsertAllOnSubmit() and I encounter the same problem - only the last record (DeveloperSelfRating object) in the 'ratings' list is being inserted into the database.
What might be the reason for this problem?
Thanks !!!
You just put this line db.SubmitChanges(); in foreach loop like
public static void InsertSelfRatings(List ratings)
{
foreach (DeveloperSelfRating sr in ratings)
{
db.DeveloperSelfRatings.InsertOnSubmit(sr);
db.SubmitChanges();
}
}
Because when u insert one record in linq you have to do submit changes so after every insert you have to write db.SubmitChanges(); just try this ok
精彩评论