i have this code, that’s replicating without any reason, my issue is when I try to change values in ‘NewRow’ its automatically gets copied to ‘CurRow’, I want to create copies of ‘CurRow’ and each row has a different value in Gmtrs,weight.
var CurRow = db.Fold_Trans.AsNoTracking().First(x => x.ID == Id);
List<Fold_Trans> LsFoldTrans = [CurRow];
var OrgGmtrs = decimal.Parse(TxtOrgGMtrs.EditValue);
var TpMtrs = decimal.Parse(TxtTPGMtrs.EditValue);
var BalMtrs = decimal.Parse(OrgGmtrs - TpMtrs);
var OrgWt = decimal.Parse(TxtOrgWt.EditValue);
var OrgAvgWt = decimal.Parse(TxtOrgAvgWt.EditValue);
var TpWt = decimal.Parse(OrgAvgWt * TpMtrs);
var BalWt = decimal.Parse(OrgWt - TpWt);
var NewRow = CurRow;
CurRow.ID = Id;
CurRow.Gmtrs = BalMtrs;
CurRow.Weight = BalWt;
int ExCn = 1;
string NewPieceNo = CurRow.Piece + @$"-TP{ExCn}";
string NewMainPieceNo = CurRow.MainPiece + @$"-TP{ExCn}";
NewRow.Piece = NewPieceNo;
NewRow.MainPiece = NewMainPieceNo;
NewRow.Gmtrs = TpMtrs;
NewRow.Weight = TpWt;
NewRow.ID = 0;
LsFoldTrans.Add(NewRow);
db.Fold_Trans.AddUpdateOrDeleteCommRange(LsFoldTrans, x => x.ID == Id);
db.SaveChanges();
How can this behavior be prevented and what do i need to do so this doesn’t happen again in whole project.
im using .net 8 with EF core and SQL server.
1
The issue you’re experiencing is because NewRow is referencing the same object as CurRow. In C#, when you assign one object to another, they both reference the same memory location. To create a true copy of the object, you need to clone it. Since Fold_Trans is an Entity Framework entity, you can manually create a deep copy of the object.
Here’s an example of how you can create a copy of CurRow:
- Create a method to clone your Fold_Trans object.
- Use this method to create a new instance for NewRow.
Below is the modified code:
var CurRow = db.Fold_Trans.AsNoTracking().First(x => x.ID == Id);
List<Fold_Trans> LsFoldTrans = new List<Fold_Trans> { CurRow };
var OrgGmtrs = decimal.Parse(TxtOrgGMtrs.EditValue.ToString());
var TpMtrs = decimal.Parse(TxtTPGMtrs.EditValue.ToString());
var BalMtrs = OrgGmtrs - TpMtrs;
var OrgWt = decimal.Parse(TxtOrgWt.EditValue.ToString());
var OrgAvgWt = decimal.Parse(TxtOrgAvgWt.EditValue.ToString());
var TpWt = OrgAvgWt * TpMtrs;
var BalWt = OrgWt - TpWt;
// Clone the CurRow object to create a new instance for NewRow
var NewRow = CloneFoldTrans(CurRow);
CurRow.Gmtrs = BalMtrs;
CurRow.Weight = BalWt;
int ExCn = 1;
string NewPieceNo = CurRow.Piece + @$"-TP{ExCn}";
string NewMainPieceNo = CurRow.MainPiece + @$"-TP{ExCn}";
NewRow.Piece = NewPieceNo;
NewRow.MainPiece = NewMainPieceNo;
NewRow.Gmtrs = TpMtrs;
NewRow.Weight = TpWt;
NewRow.ID = 0;
LsFoldTrans.Add(NewRow);
db.Fold_Trans.AddUpdateOrDeleteCommRange(LsFoldTrans, x => x.ID == Id);
db.SaveChanges();
Fold_Trans CloneFoldTrans(Fold_Trans source)
{
return new Fold_Trans
{
// Copy all the properties from source to new instance
Piece = source.Piece,
MainPiece = source.MainPiece,
Gmtrs = source.Gmtrs,
Weight = source.Weight,
// Copy other properties as needed
// Ensure not to copy the ID if you want a new row in the database
// Exclude any properties that should not be copied
// You may also want to include any navigation properties if needed
};
}
In this code:
- CloneFoldTrans method creates a new instance of Fold_Trans and copies
the values from the source object. - NewRow is created by cloning CurRow to ensure they do not reference
the same object. - CurRow and NewRow can then be independently modified without
affecting each other.
Khach is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1