Peter
Peter

Reputation: 803

LINQ Multiple Insertion

I have the under listed function

 public bool Card_Gen(int id) {
    Card_Details New_Card = new Card_Details();
    int k =0;
    try
    {
        for (k = 1; k <= id; k++)
        {                                  
                New_Card.Card_Num = CreateMD5Hash(Card_Number()).Substring(0, 12);
                New_Card.Card_Serial = Card_Serial();
                db.Card_Details.AddObject(New_Card);
                db.SaveChanges();
         }
    }
    catch (Exception ex) {
        return false;
    }
    return true;
}

But instead of inserting the index times it only does a single insert. What am I doing wrong?

Upvotes: 0

Views: 66

Answers (4)

Benjamin RD
Benjamin RD

Reputation: 12034

You can use db.Configuration.AutoDetectChangesEnabled to enable flush data in the database.

for (k = 1; k <= id; k++)
        {                                  
                New_Card.Card_Num = CreateMD5Hash(Card_Number()).Substring(0, 12);
                New_Card.Card_Serial = Card_Serial();
                db.Card_Details.AddObject(New_Card);
                db.SaveChanges();
                //db.SubmitChanges();
                db.Configuration.AutoDetectChangesEnabled = true; 

         }

or try using

db.Flush()

Upvotes: 0

Tom D&#39;Hulster
Tom D&#39;Hulster

Reputation: 123

Have you tried moving the instantiation of your object inside your for loop? Right now, it looks like youre editing the same object over and over again.

Will edit later with example if necessary, I'm on my phone at the moment.

Upvotes: 0

Reza
Reza

Reputation: 19913

You should create new instance of object every time, other wise it will update old rows

 public bool Card_Gen(int id) {
    Card_Details New_Card ;
    int k =0;
    try
    {
        for (k = 1; k <= id; k++)
        {
                //creating new object in loop
                New_Card = new Card_Details();

                New_Card.Card_Num = CreateMD5Hash(Card_Number()).Substring(0, 12);
                New_Card.Card_Serial = Card_Serial();

                //edit in these two lines if you are using LINQ2SQL otherwise for other providers your code might be correct 
                db.Card_Details.InsertOnSubmit(New_Card);
                db.SubmitChanges();
         }
    }
    catch (Exception ex) {
        return false;
    }
    return true;
}

Upvotes: 0

Omri Aharon
Omri Aharon

Reputation: 17064

You can't do that. You define your New_Card outside the loop, therefore you are only inserting it once on the first loop iteration and updating it on the next ones.

Your code should be:

public bool Card_Gen(int id) {

    int k =0;
    try
    {
        for (k = 1; k <= id; k++)
        {                                  
                Card_Details New_Card = new Card_Details();
                New_Card.Card_Num = CreateMD5Hash(Card_Number()).Substring(0, 12);
                New_Card.Card_Serial = Card_Serial();
                db.Card_Details.AddObject(New_Card);
                db.SaveChanges();
         }
    }
    catch (Exception ex) {
        return false;
    }
    return true;
}

Upvotes: 1

Related Questions