1

I'm trying to insert all the ith values to table called AB_Product_vs_Field but its only insert last i values only

this is that LINQ query

for (int i = 0; i < product.ListProductFields.Count; i++)
{
   insertproductvalue.Product_ID = product.Product_ID;
   insertproductvalue.Field_ID = product.ListProductFields[i].ProductFieldID;
   insertproductvalue.Field_Value_EN = product.ListProductFields[i].Field_Value_EN;                         
};

db.AB_Product_vs_Field.Add(insertproductvalue);
db.SaveChanges();

How to insert all of those rows one by one

4
  • 2
    Put db.AB_Product_vs_Field.Add(insertproductvalue); inside the for loop (your currently only adding the last one). And you really should initialize a new instance of insertproductvalue inside the for loop as well. Commented Nov 16, 2015 at 8:58
  • @StephenMuecke then I'm getting this SystemInvalidOperationException: {"The property 'Field_ID' is part of the object's key information and cannot be modified. "} Commented Nov 16, 2015 at 9:03
  • Then if its the PK field, remove insertproductvalue.Product_ID = product.Product_ID; (it will be generated automatically when you save) - or are you editing existing objects? Commented Nov 16, 2015 at 9:05
  • Product_ID and Field_ID are the composite primary key here, those are varchar fields, user have to insert Product_ID using frond end view page , Field_ID im taking using following line in view page @Html.HiddenFor(m => m.ListProductFields[i].ProductFieldID) Commented Nov 16, 2015 at 9:11

3 Answers 3

3

I can't see how you create your insertproductvalue but i suppose that your code should looks like this:

var listInsertproductvalue = new List<AB_Product_vs_Field>(); //you creating list of your entities
for (int i = 0; i < product.ListProductFields.Count; i++)
{
   listInsertproductvalue.Add(new AB_Product_vs_Field 
   {
      Product_ID = product.Product_ID, 
      Field_Value_EN = product.ListProductFields[i].Field_Value_EN
   }); //here you create your entity and add it to List
};
db.AB_Product_vs_Field.AddRange(listInsertproductvalue); //here you add all new entities to context
db.SaveChanges();

Here you create empty List of your entities, fill it and then add list to your Context with AddRange() method.

Sign up to request clarification or add additional context in comments.

3 Comments

once I use your solution its saving last ith value , I checked it twice, with DB , what should I do
@kez i updated answer. You always can create list of entities and then use AddRange.
@teovankot in this code snippet I have defined insertproductvalue object like below AB_Product_vs_Field insertproductvalue = new AB_Product_vs_Field(); . can I use that object instead of var listInsertproductvalue = new List<AB_Product_vs_Field>(); here ?
0

You juste have to put that line :

db.AB_Product_vs_Field.Add(insertproductvalue);

In the for and keep the save outside the for.

2 Comments

once I use your solution its saving last ith value
try to saveChange on the for. Could solve the problem.
0

I am assuming few thing here, but it has a very short and sweet way of doing it. I am a little bit blind as I need more information on the code, that is variable declarations

Lets say product.ListProductFields is your List of some object which has all the rows to be inserted:

First Way :

product.ListProductFields.Foreach(objEach=>{
db.AB_Product_vs_Field.Add((objEach); /* First we can get, .productId, .FeildId etc inside the function*/
// OR 
// db.AB_Product_vs_Field.Add((objEach.Product_ID, objEach.ProductFieldID, objEach.Field_Value_EN;);

});
db.SaveChanges();

There is one more easier way that is to have a List<<<insertproductvalue>>> where it is list of your custom class object where each object will be added with properties and the object will be added to the list of you custom class object and then again we can use the upper code to insert the rows into db.

Second Way :

var lstinsertproductvalue=new List<<*CLASSTYPE*>>();
for (int i = 0; i < product.ListProductFields.Count; i++)
            {
insertproductvalue=new <<*CLASSTYPE*>>();
                insertproductvalue.Product_ID = product.Product_ID;
                insertproductvalue.Field_ID = product.ListProductFields[i].ProductFieldID;
                insertproductvalue.Field_Value_EN = product.ListProductFields[i].Field_Value_EN;
lstinsertproductvalue.Add(insertproductvalue); 
// Here you have created a list your-self assuming that product.ListProductFields is not a list. 
            };

lstinsertproductvalue.Foreach(objEach=>{
db.AB_Product_vs_Field.Add((objEach); /* First we can get, .productId, .FeildId etc inside the function*/
// OR 
// db.AB_Product_vs_Field.Add((objEach.Product_ID, objEach.ProductFieldID, objEach.Field_Value_EN;);    
});
db.SaveChanges();

5 Comments

that's not very good idea call db.SaveChanges(); on each entity add.
getting this error 'IList<AB_ProductTypeCategoryField>' does not contain a definition for 'Foreach' and no extension method 'Foreach' accepting a first argument of type 'IList<AB_ProductTypeCategoryField>' could be found
@kez ; Can you please give me the complete related code that is the variable declaration too, please as it will really help me to update you properly.
@teovankot ; Thanks I had forgotten to take it outside anyways have updated the code accordingly.
@kez : You can try the second method, I was having a feeling that this error might occur, so only had edited the post and kept the second way too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.