0

while inserting records in a loop The property "id" is part of the object's key information and cannot be modified

 secProductionRepository.Add(tblSecProduction);
 this.SaveChanges();

CODE

Controller : This is the Controller code from where i am calling method of Repository . Adding data into repository and calling function to insert. i think i have to initialize it every time with new keyword. But where should i do that.

SingleMaster objGetXMLData = _iSingleService.GetXMLData();
if (objGetXMLData._tblSecDoorXMLData != null)
{
    for (int totalCount = 0; totalCount < objGetXMLData._tblSecDoorXMLData.Count; totalCount++)
    {
        _tblSecDoorsProduction.txtTongue = singleDoorModel.txtTongue;
        _tblSecDoorsProduction.numFibMesh = Convert.ToInt32(singleDoorModel.chkBoxFibreMesh);
        _tblSecDoorsProduction.dteDesDate = DateTime.Now;
        _iSingleDoorService.UpdatetblSecDoorsProduction(_tblSecDoorsProduction, "Insert");
    }
}

Repository : Here i am inserting new row into the table

public void UpdatetblSecDoorsProduction(tblSecDoorsProduction  tblSecDoorsProduction, string Message)
{
    var secDoorsProductionRepository =   Nuow.Repository<tblSecDoorsProduction>();
    tblSecDoorsProduction alreadyAttached = null;
    if (Message == "Insert")
    {
        secDoorsProductionRepository.Add(tblSecDoorsProduction);
        Nuow.SaveChanges();
    }
}
11
  • Can you show whole code? Commented Jan 18, 2016 at 12:36
  • var secDoorsProductionRepository = Nuow.Repository<tblSecDoorsProduction>(); secDoorsProductionRepository.Add(tblSecDoorsProduction); Nuow.SaveChanges(); Commented Jan 18, 2016 at 12:38
  • Could you show the whole code, as dawidr already mentioned? Where is the Loop? Are you creating a new repository in the Loop? We can't see anything :-D Commented Jan 18, 2016 at 12:41
  • More code:) Update you question with whole method. Commented Jan 18, 2016 at 12:41
  • After insering one record, When it comes to next it throws error Something Like this Commented Jan 18, 2016 at 12:42

1 Answer 1

1

Create new object each time in the loop. Updated code here:

for (int totalCount = 0; totalCount < objGetXMLData._tblSecDoorXMLData.Count; totalCount++)
{
    tblSecDoorsProduction _tblSecDoorsProduction = new tblSecDoorsProduction();
    _tblSecDoorsProduction.txtTongue = singleDoorModel.txtTongue;
    _tblSecDoorsProduction.numFibMesh = Convert.ToInt32(singleDoorModel.chkBoxFibreMesh);
    _tblSecDoorsProduction.dteDesDate = DateTime.Now;
    _iSingleDoorService.UpdatetblSecDoorsProduction(_tblSecDoorsProduction, "Insert");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dawidr .. worked for me. I had created object outside loop.

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.