I am writing a program for accounting for suppliers, and orders from these suppliers using the Entity Framework Code First.

Simultaneously with the creation of an order, a new supplier is created with this order, that is, in the database there is a supplier with the order and a supplier without an order, if you create another order, another supplier will be created with this order.

Classes:

Поставщик :

 public class Contractor { public int ContractorId { get; set; } public string ContractorName { get; set; } public string ManagerName { get; set; } public string ContactPhone { get; set; } public string ContactEmail { get; set; } public ICollection<Order> Orders { get; set; } public override string ToString() { return ContractorName; } } 

Заказ :

 public class Order { public int OrderId { get; set; } public virtual ICollection<PositionsQty> PositionsQty { get; set; } public decimal Sum { get; set; } public string InvoiceNumber { get; set; } public Byte[] InvoiceScan { get; set; } public Byte[] ClosingDocScan { get; set; } public DateTime OrderDate { get; set; } public DateTime DeliveryDate { get; set; } public int ContractorId { get; set; } public virtual Contractor Contractor { get; set; } public override string ToString() { return "Счет №: " + InvoiceNumber + " от " + OrderDate.Day + "/" + OrderDate.Month + "/" + OrderDate.Year; } } 

Create Заказа :

  private void SaveBtn_Click(object sender, RoutedEventArgs e) { using (var db = new ContractorContext()) { Order order = new Order(); if(InvoiceNumberBox.Text != null) order.InvoiceNumber = InvoiceNumberBox.Text; order.DeliveryDate = DeliveryDatePick.SelectedDate ?? DateTime.Now; order.OrderDate = OrderDatePick.SelectedDate ?? DateTime.Now; if(InvoiceSumBox.Text != null) order.Sum = Decimal.Parse(InvoiceSumBox.Text.Replace('.',',')); order.Contractor = _contractor; order.ContractorId = _contractor.ContractorId; order.PositionsQty = new List<PositionsQty>(); foreach (PositionsQty item in OrderPositions.Items) { order.PositionsQty.Add(item); } if (_contractor.Orders != null) { _contractor.Orders.Add(order); } else { _contractor.Orders = new List<Order> {order}; } //db.Contractors.AddOrUpdate(_contractor); db.Orders.AddOrUpdate(order); db.SaveChanges(); this.Close(); } } 

The _contractor object _contractor passed from another form.

Where is the mistake?

    2 answers 2

    The order and _contractor are in different instances of the ContractorContext context. In order not to produce duplicates, do not fill out the field order.Contractor , it is enough to specify order.ContractorId .

    • Removed the field and everything works as it should, thanks - ajlewa
    • @ajlewa and maybe it’s worth adding a check that excludes the possibility of duplication, it’s checked if you don’t catch such situations, then this (duplication) will surely happen - Bald

    And in your base structure for the Contractor table, the primary key was created as ContractorId? Perhaps you will be helped by the explicit indication of keys for Entity Framevork, try this

    For the supplier:

     public class Contractor { [Key] public int ContractorId { get; set; } ... } 

    For order:

     public class Order { [Key] public int OrderId { get; set; } ... [ForeignKey("Contractor ")] public int ContractorId { get; set; } public virtual Contractor Contractor { get; set; } ... } 
    • The primary key in the database was generated correctly, the problem is solved, but thanks for the answer - ajlewa