I am trying to update an entity named SaleLedger with some relationships in it. But while calling Context.SaveChanges() it throws an exception
I am using EF 6.4.4 and SQL Server database
My Entity is
public class SaleLedger
{
[DisplayName("Inv. Code")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[DisplayName("Customer")]
[StringLength(100)]
public string? CustomerName { get; set; }
[DisplayName("Sale Category Id")]
public int? CatId { get; set; }
[DisplayName("Customer Id")]
public int? CustId { get; set; }
[DisplayName("Salesman Id")]
public int? SalesManId { get; set; }
[DisplayName("SalesMan Name")]
[StringLength(100)]
public string? SalesManName { get; set; }
public DateTime? Date { get; set; }
[DisplayName("Disc %")]
[Column(TypeName ="decimal")] [DecimalPrecision(5,2)]
public decimal DiscPerc { get; set; }
[DisplayName("Disc")]
[Column(TypeName = "decimal")] [DecimalPrecision(18,2)]
public decimal DiscVal { get; set; }
[DisplayName("Flat Disc")]
[Column(TypeName = "decimal")] [DecimalPrecision(18,2)]
public decimal FlatDisc { get; set; }
[DisplayName("+")]
[Column(TypeName = "decimal")] [DecimalPrecision(18,2)]
public decimal MiscCharges { get; set; }
[DisplayName("GST %")]
[Column(TypeName = "decimal")] [DecimalPrecision(5,2)]
public decimal SalesTaxPerc { get; set; }
[DisplayName("GST")]
[Column(TypeName = "decimal")] [DecimalPrecision(18,2)]
public decimal SalesTaxVal { get; set; }
[StringLength(1)]
public string? Posted { get; set; }
[DisplayName("Posting Date")]
public DateTime? PostDate { get; set; }
[StringLength(200)]
public string? Remarks { get; set; }
public DateTime? OriginalDate { get; set; }
[DisplayName("Posted By User Id")]
public short? PostedByUserId { get; set; }
[DisplayName("Posted By")]
[StringLength(100)]
public string? PostedByName { get; set; }
[DisplayName("Posted By Machine Id")]
public short? PostedByMachineId { get; set; }
[DisplayName("Posted By Machine")]
[StringLength(100)]
public string? PostedByMachineName { get; set; }
[DisplayName("Printed")]
public int RePrintingCounter { get; set; }
[DisplayName("Received Cash")]
[Column(TypeName ="decimal")] [DecimalPrecision(18,2)]
public decimal? CashReceived { get; set; }
[DisplayName("Print Warranty")]
[StringLength(1)]
public string? PrintWaranty { get; set; }
[DisplayName("Print Batch")]
[StringLength(1)]
public string? PrintBatch { get; set; }
[DisplayName("Inv. Size")]
[StringLength(100)]
public string? InvoiceSize { get; set; }
[DisplayName("Inv. Total")]
[Column(TypeName ="decimal")] [DecimalPrecision(18,2)]
public decimal? InvTotal { get; set; }
[DisplayName("Modif. Count")]
public int? ModifyCounter { get; set; }
[DisplayName("Paid Status")]
[StringLength(100)]
public string? PaidStatus { get; set; }
[DisplayName("Billing Date")]
public DateTime? BillingDate { get; set; }
[DisplayName("UnReceived Balance")]
[Column(TypeName ="decimal")] [DecimalPrecision(18,2)]
public decimal? UnReceivedBalance { get; set; }
[DisplayName("Total Of Sale Returns")]
[Column(TypeName ="decimal")] [DecimalPrecision(18,2)]
public decimal? TotalOfSaleReturns { get; set; }
[DisplayName("Outstanding")]
[Column(TypeName ="decimal")] [DecimalPrecision(18,2)]
public decimal? OutStandingAmt { get; set; }
[DisplayName("Print Balance")]
[StringLength(1)]
public string? PrintBalance { get; set; }
[Column(TypeName ="decimal")] [DecimalPrecision(18,2)]
public decimal? Balance { get; set; }
[StringLength(1)]
public string? Deleted { get; set; }
[DisplayName("AccountCode")]
public string? AccountCode { get; set; }
[DisplayName("Sale Order Id")]
public int? SaleOrderId { get; set; }
[DisplayName("Due Date")]
public DateTime? DueDate { get; set; }
[StringLength(100)]
[DisplayName("Sale Category")]
public string? SaleCategoryName { get; set; }
[StringLength(1)]
public string? PrintExpiry { get; set; }
[ForeignKey("CatId")]
[InverseProperty("Ledgers")]
public virtual SaleCategory? Category { get; set; }
[InverseProperty("Ledger")]
public virtual List<SaleDetail>? Details { get; set; }
[DisplayName("Type Id")]
public short? TypeId { get; set; }
[InverseProperty("Ledgers")]
[ForeignKey("TypeId")]
public virtual SaleType? Type { get; set; }
[ForeignKey("CustId")]
[InverseProperty("SaleLedgers")]
public virtual Customer? Customer { get; set; }
[ForeignKey("SalesManId")]
[InverseProperty("SaleLedgers")]
public virtual Salesman? SalesMan { get; set; }
[NotMapped]
[DisplayName("No.")]
public int SrNo { get; set; }
[StringLength(100)]
public string? Name { get; set; }
[DisplayName("Saved By Id")]
public short? SavedById { get; set; }
[DisplayName("Saved By Machine Id")]
public short? SavedByMachineId { get; set; }
[DisplayName("Saved Date")]
public DateTime? SavedDate { get; set; }
[StringLength(100)]
[DisplayName("Saved By")]
public string? SavedByName { get; set; }
[StringLength(100)]
[DisplayName("Saved By Machine")]
public string? SavedByMachineName { get; set; }
[StringLength(1)]
public string? Active { get; set; }
[ForeignKey("SavedById")]
public virtual User? SavedByUser { get; set; }
[ForeignKey("SavedByMachineId")]
public virtual Machine? SavedByMachine { get; set; }
}
SaleDetail Entity is
public class SaleDetail
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[DisplayName("Sale Item Id")]
public int? SItemId { get; set; }
[DisplayName("Sale Item Name")]
[StringLength(100)]
public string? SItemName { get; set; }
[StringLength(200)]
public string? Batch { get; set; }
public DateTime? Expiry { get; set; }
public int? Qty { get; set; }
public int? Packs { get; set; }
[DisplayName("Pack Units")]
public int? PackUnits { get; set; }
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
[DisplayName("Pcs Sale Amount")]
public decimal? PcsSaleAmount { get; set; }
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? Rate { get; set; }
[Column(TypeName = "decimal")]
[DecimalPrecision(5, 2)]
[DisplayName("Disc %")]
public decimal? DiscPerc { get; set; }
[Column(TypeName = "decimal")]
[DecimalPrecision(5, 2)]
[DisplayName("Sales Tax")]
public decimal? SalesTax { get; set; }
[DisplayName("Row Id")]
public int? RowID { get; set; }
[DisplayName("Due Qty")]
public int? DueQty { get; set; }
[DisplayName("Satisfied Date")]
public DateTime? SatisfiedDate { get; set; }
[DisplayName("Pack Sale Amount")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? PackSaleAmount { get; set; }
[DisplayName("Unit Sales Tax")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? UnitSalesTax { get; set; }
[DisplayName("GST %")]
[Column(TypeName = "decimal")]
[DecimalPrecision(5, 2)]
public decimal? GSTPerc { get; set; }
[DisplayName("Pack Bonus Qty")]
public int? PackBonusQty { get; set; }
[DisplayName("Pcs Bonus Qty")]
public int? PcsBonusQty { get; set; }
[DisplayName("Pur Unit Rate")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? PurUnitRate { get; set; }
[DisplayName("MRP Unit Rate")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? MRPUnitRate { get; set; }
[DisplayName("Gross Amount")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? GrossAmount { get; set; }
[DisplayName("Add %")]
[Column(TypeName = "decimal")]
[DecimalPrecision(5, 2)]
public decimal? AddPerc { get; set; }
[DisplayName("Add Value")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? AddVal { get; set; }
[DisplayName("Disc Value")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? DiscVal { get; set; }
[DisplayName("Net Amount")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? NetAmount { get; set; }
[DisplayName("GST Value")]
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
public decimal? GSTVal { get; set; }
[DisplayName("Sale Inv Id")]
public int? SaleInvId { get; set; }
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
[DisplayName("Flat Disc.")]
public decimal? FlatDisc { get; set; }
[Column(TypeName = "decimal")]
[DecimalPrecision(18, 2)]
[DisplayName("Total Disc")]
public decimal? TotalDiscVal { get; set; }
[DisplayName("Initial Packs Stock")]
public int? InitialPackStock { get; set; }
[DisplayName("Initial Pcs Stock")]
public int? InitialPcsStock { get; set; }
[DisplayName("Balance Packs Stock")]
public int? BalancePackStock { get; set; }
[DisplayName("Balance Pcs Stock")]
public int? BalancePcsStock { get; set; }
[InverseProperty("Details")]
[ForeignKey("SaleInvId")]
public virtual SaleLedger? Ledger { get; set; }
[ForeignKey("SItemId")]
public virtual Item? SItem { get; set; }
}
Actually I am trying to update an entity named SaleLedger with some relationships in it.
While calling Context.SaveChanges() in update
It throws an exception saying.
System.InvalidOperationException
HResult=0x80131509
Message=Multiplicity constraint violated. The role 'SaleDetail_Ledger_Target' of the relationship 'BLL.SaleDetail_Ledger' has multiplicity 1 or 0..1.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Objects.DataClasses.EntityReference`1.VerifyNavigationPropertyForAdd(IEntityWrapper wrapper)
at System.Data.Entity.Core.Objects.EntityEntry.AddRelationshipDetectedByGraph(Dictionary`2 relationships, Object relatedObject, RelatedEnd relatedEndFrom, Boolean verifyForAdd)
at System.Data.Entity.Core.Objects.EntityEntry.DetectChangesInRelationshipsOfSingleEntity()
at System.Data.Entity.Core.Objects.ObjectStateManager.DetectChangesInNavigationProperties(IList`1 entries)
at System.Data.Entity.Core.Objects.ObjectStateManager.DetectChanges()
at System.Data.Entity.Core.Objects.ObjectContext.DetectChanges()
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate)
at System.Data.Entity.Internal.InternalContext.GetStateEntries()
at System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
at System.Data.Entity.DbContext.GetValidationErrors()
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at DAL.SaleLedgerDAL.SaveSaleLedger(SaleLedger cust) in E:\Aamir\Ehsan\MedTradeSolutionWinforms\DAL\SaleLedgerDAL.cs:line 290
at DesktopUI.FrmSaleInvoice.SaveInvoice() in E:\Aamir\Ehsan\MedTradeSolutionWinforms\DesktopUI\FrmSaleInvoice.cs:line 713
at DesktopUI.FrmSaleInvoice.BtnSave_Click(Object sender, EventArgs e) in E:\Aamir\Ehsan\MedTradeSolutionWinforms\DesktopUI\FrmSaleInvoice.cs:line 1907
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DesktopUI.Program.Main() in E:\Aamir\Ehsan\MedTradeSolutionWinforms\DesktopUI\Program.cs:line 19