开发者

linq to sql (VS 2010) - insert doesn't work

开发者 https://www.devze.com 2023-03-09 15:06 出处:网络
I have a class containing my data context: private static drDataContext db = new drDataContext(); I also have the method:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号