I got a problem when I’m trying to insert new record by EF Core 7 the related models

I have 2 model classes:

public class MoReg
{
    #region properties
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int RegID { get; set; }

    [Required]
    public long RegRef { get; set; }

    [Required]
    public DateTime? RegDate { get; set; }

    [Required]
    public int? RegType { get; set; }

    [Required]
    public bool RegDeported { get; set; }

    [Required]
    public string? RegNotes { get; set; }

    //relations
    public MoAtt xType { get; set; } = null!;

    public List<MoRegDet> xRegDetList { get; set; }
    #endregion
}

public class MoRegDet
{
    #region properties
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int RegDetID { get; set; }

    [Required]
    public long RegDetRef { get; set; }

    [Required]
    public int RegIDx { get; set; } // المفتاح الأجنبي

    [Required]
    public DateTime? RegDetDate { get; set; }

    [Required]
    public double? RegDetMadeen { get; set; }

    [Required]
    public double? RegDetDaen { get; set; }

    [Required]
    public int? RegDetAcc { get; set; }

    [Required]
    public int? RegDetCurr { get; set; }

    [Required]
    public double? RegDetRate { get; set; }

    [Required]
    public double? RegDetMadeen2 { get; set; }

    [Required]
    public double? RegDetDaen2 { get; set; }

    [Required]
    public string? RegDetNotes { get; set; }

    // relations
    public MoAcc xAcc { get; set; } = null!;
    public MoCurrency xCurr { get; set; } = null!;

    // navigation properties
    [ForeignKey(nameof(RegIDx))]
    public MoReg xReg { get; set; } = null!;
    #endregion
}

I have a DbContext file called AppDb with the following code:

//Regs
modelBuilder.Entity<MoReg>(entity =>
 {
     entity.ToTable("regs");
     entity.HasKey(e => e.RegID);
     entity.Property(e => e.RegID);
     entity.Property(e => e.RegRef).IsRequired();
     entity.Property(e => e.RegDate).IsRequired();
     entity.Property(e => e.RegType).IsRequired();
     entity.Property(e => e.RegDeported).IsRequired();
     entity.Property(e => e.RegNotes).IsRequired(false);

     entity.HasOne(e => e.xType)
          .WithMany()
          .HasForeignKey(e => e.RegType)
          .OnDelete(DeleteBehavior.Restrict);
 });

// Regs det
modelBuilder.Entity<MoRegDet>(entity =>
 {
     entity.ToTable("regs_det");
     entity.HasKey(e => e.RegDetID);
     entity.Property(e => e.RegDetID);
     entity.Property(e => e.RegDetRef).IsRequired();
     entity.Property(e => e.RegIDx).IsRequired();
     entity.Property(e => e.RegDetDate).IsRequired();
     entity.Property(e => e.RegDetMadeen).IsRequired();
     entity.Property(e => e.RegDetDaen).IsRequired();
     entity.Property(e => e.RegDetAcc).IsRequired();
     entity.Property(e => e.RegDetCurr).IsRequired();
     entity.Property(e => e.RegDetRate).IsRequired();
     entity.Property(e => e.RegDetMadeen2).IsRequired();
     entity.Property(e => e.RegDetDaen2).IsRequired();
     entity.Property(e => e.RegDetNotes).IsRequired(false);

     entity.HasOne(e => e.xReg)
          .WithMany(reg => reg.xRegDetList)
          .HasForeignKey(e => e.RegIDx)
          .OnDelete(DeleteBehavior.Cascade);

     entity.HasOne(e => e.xAcc)
          .WithMany()
          .HasForeignKey(e => e.RegDetAcc)
          .OnDelete(DeleteBehavior.Restrict);

     entity.HasOne(e => e.xCurr)
          .WithMany()
          .HasForeignKey(e => e.RegDetCurr)
          .OnDelete(DeleteBehavior.Restrict);
});

I created a manager file RegsManager that has the following code:

// Reg
public List<MoReg> xRegsList;
public MoReg xReg { get; set; }

// Reg Det
// public List<MoRegDet> xRegsDetList;
public MoRegDet xRegDet { get; set; }

// Others
public AttsManager _Mngr_Atts;
public AccManager _Mngr_Accs;
public CurrenciesManager _Mngr_Currs;
public RatesManager _Mngr_Rates;

#region insert
public void Insert(AppDb xDb, MoReg xNew)
{
     using (var transaction = xDb.Database.BeginTransaction())
     {
         try
         {
             xDb.Db_MoRegs.Add(xNew);
             xDb.SaveChanges();

             foreach (var detail in xNew.xRegDetList)
             {
                 detail.RegIDx = xNew.RegID;
             }

             xDb.Db_MoRegsDet.AddRange(xNew.xRegDetList);
             xDb.SaveChanges();

             transaction.Commit();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
             transaction.Rollback();
         }
     }
}
#endregion

When I run the following code when I click on the “Save” button:

// set data
_Manager.xReg = new MoReg()
 {
     RegRef = _Manager.GetRef(),
     RegDate = TxtRegDate.Value,
     RegType = 1,
     RegDeported = RdYes.Checked ? true : false,
     RegNotes = TxtRegNotes.Text.Trim(),
     xRegDetList = (List<MoRegDet>)Dgv.DataSource

 };

AppDb xDb = new AppDb();

if (xDb.IsConnected())
{
     _Manager.Insert(xDb, _Manager.xReg);
     _Manager.xOpResult = AppEnums.Errs.NoErr;
}

The problem is when I clear the line:

xRegDetList = (List<MoRegDet>)Dgv.DataSource

and just run:

xDb.Db_MoRegs.Add(xNew);
xDb.SaveChanges();

I can save just the header but if I keep it (xRegDetList = (List<MoRegDet>)Dgv.DataSource) I get an error

Could not save changes. Please configure your entity type accordingly

I tried for more than 5 hours I didn’t get any method to make it work correctly.

I hope someone can help me.

I tried to insert just the header and it worked and I tried by AI and nothing worked.

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