0

I am trying to use InsertAllOnSubmit to do multiple inserts, but only 1 item ever gets inserted into the table. The only reason I can think that this is happening is something to do with the generation of the Primary Key field, I am currently allowing the code to auto generate this field, and it doesn't seem to be doing that. Can anybody help?

List<rewardsClaimed> lstRewardsClaimed = new List<rewardsClaimed>();

for (int i = 0; i < ticket.delivery.quantity; i++)
{
    rewardsClaimed claim = result;
    lstRewardsClaimed.Add(claim);
}
dc.rewardsClaimeds.InsertAllOnSubmit(lstRewardsClaimed);
dc.SubmitChanges();

EDIT :

I have found out what the problem is....the ID field gets set to 0 by default, so that when the insert is done, since all rows have the same ID, they are treated as 1 row. How would I prevent this?

user517406
  • 13,623
  • 29
  • 80
  • 120

4 Answers4

2

The problem is that List and Table behave completely different.

A List<T> allows you to add the same instance more than once without checking the reference in other words in your list you have multiple references to the same object.

Table<T> in the other hand when you call InsertAllOnsubmit method checks every item in the collection to see if its already "marked" to be inserted in the table. If the object its already "marked" skips it.

What is happening with your code is that you are passing the List of claims that make reference n number of times to the same object. When is passed to Table<T> the first ocurrence is marked for insertion. Since the other items in the list are the same object, they are already marked for insertion.

Hope this clarifies.

Arturo Martinez
  • 3,737
  • 1
  • 22
  • 35
1

Everyone is asking to create new instance of the class RewardsClaimed. But I thought that it is not the correct reason. Hence I tried the following sample code and found that 5 different instances are added in the list.

Part 1: Showing that same item can be added to local list without creating new instance (Its not the answer)

Code: enter image description here

Results:

enter image description here

Part 2: Creation of new primary key will be the solution. (This is the issue and solution.)

I would suggest that, please check the data in your PrimaryID Column is not repeated. By this I mean, if your primary key is not automatically generated and you generate it manually, chances are it gets rows with same primarykeys n number of times. If it is repeated then it will only insert single instance of it.

Edit: How to auto generate Field Value

If you want to auto-generate primary key refer this.

Community
  • 1
  • 1
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • Yes, this is the issue I am having. But I don't know why or how to prevent the Primary Column data being repeated. – user517406 Apr 19 '12 at 07:31
  • 1
    you can prevent it by autogenerating the primaryId in SQL Database side. Or else create new instance of result, which has different values of primary id. – Marshal Apr 19 '12 at 07:33
  • can you show us table structure of you RewardsClaimed table ? – Marshal Apr 19 '12 at 07:34
  • @user517406 Only creating new instance will not prevent the problem. But he must create new instance with new PrimaryKey value – Marshal Apr 19 '12 at 07:48
  • I have found out what the problem is....the ID field gets set to 0 by default, so that when the insert is done, since all rows have the same ID, they are treated as 1 row. How would I prevent this? – user517406 Apr 19 '12 at 12:35
  • @user517406: This is what I am trying to say since long. Please provide how you set the `result` variable and also it would be better to know about Properties in `rewardsClaimed` Entity. – Marshal Apr 19 '12 at 12:36
  • @Marshal, When you instantiate a new string and the actual text is the same you are not creating a new string, you are just creating a new reference to the same object in the heap. – Arturo Martinez Apr 19 '12 at 14:15
0

you are not creating new instance of claim.

rewardsClaimed claim = new rewardsClaimed();
Habib
  • 219,104
  • 29
  • 407
  • 436
  • no you dont require to create new instance everytime. I just tested it. Please see my answer. – Marshal Apr 19 '12 at 07:23
  • @Marshal Its not the issue, He is trying to create a list of Mapped Entities so that he can call insertallonsubmit with the list. Your answer is showing insertion in a simple string list. – Habib Apr 19 '12 at 07:35
  • I took string for simple execution. I agree that he is adding an Entity which would have many properties. But the actual problem is that there is no difference in the property which is primaryKey. Only creating new instance will not prevent the problem. But he must create new instance with new PrimaryKey value – Marshal Apr 19 '12 at 07:37
  • Creating the new instances within the for loop does not fix the issue – user517406 Apr 19 '12 at 07:44
  • @user517406 Refering to above comment again: Only creating new instance will not prevent the problem. But he must create new instance with new PrimaryKey value. – Marshal Apr 19 '12 at 07:47
0
List<rewardsClaimed> lstRewardsClaimed = new List<rewardsClaimed>();

for (int i = 0; i < ticket.delivery.quantity; i++)
{
    rewardsClaimed claim = new rewardsClaimed();
    claim.property = result.property; // do this foreach property except id field
    lstRewardsClaimed.Add(claim);
}

dc.rewardsClaimeds.InsertAllOnSubmit(lstRewardsClaimed);

dc.SubmitChanges();
daryal
  • 14,643
  • 4
  • 38
  • 54