Prevent variables of same type replicate values

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:

  1. Create a method to clone your Fold_Trans object.
  2. 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.
New contributor

Khach is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật