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();
db.AB_Product_vs_Field.Add(insertproductvalue);inside theforloop (your currently only adding the last one). And you really should initialize a new instance ofinsertproductvalueinside theforloop as well.{"The property 'Field_ID' is part of the object's key information and cannot be modified. "}insertproductvalue.Product_ID = product.Product_ID;(it will be generated automatically when you save) - or are you editing existing objects?Product_IDandField_IDare the composite primary key here, those are varchar fields, user have to insertProduct_IDusing frond end view page ,Field_IDim taking using following line in view page@Html.HiddenFor(m => m.ListProductFields[i].ProductFieldID)