1
votes

When i save an order from my winforms app it works ok. But when i try to add a second one the SaveChanges() method throws an exception:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

I have one table "Orders" and one "Orderlines" that has a foreignkey OrderID.

Does anyone know why this happens?

I know that my context handling is not ideal, so if that is the problem, any suggestions on how to change this would be much obliged!

public class Orders
{
    private readonly DataContext _context;

    public Orders()
    {
        _context = EntityContext.Context;
    }

    public int Save(Order order)
    {
        if (order.ID > 0)
            _context.Entry(order).State = EntityState.Modified;
        else
        {
            _context.Orders.Add(order);
        } 
            _context.SaveChanges();
            return order.ID;
    }
}
public class EntityContext : IDisposable
{
    private static DataContext _context;
    public static DataContext Context
    {
        get
        {
            if (_context != null)
                return _context;
            return _context = new DataContext();
        }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

EDIT 1 Added Order class and screencapture of order on exceptiontime

I have set update integrity to CASCADE.

Orderlines is a relationship table.

public partial class Order
{
    public Order()
    {
        this.Orderlines = new HashSet<Orderline>();
    }

    public int ID { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public int PaymentType { get; set; }
    public Nullable<int> SettlementID { get; set; }
    public Nullable<decimal> CashPayment { get; set; }
    public Nullable<decimal> BankPayment { get; set; }
    public Nullable<int> UserID { get; set; }

    public virtual ICollection<Orderline> Orderlines { get; set; }
    public virtual Settlement Settlement { get; set; }
}

Object order on exception

EDIT 2 - Partial classes

I have partial classes extending the order and orderline objects:

public partial class Order 
{
    public decimal Total
    {
        get { return Orderlines.Sum(x => x.Quantity*x.Price); }
    }
    public decimal TotalExMva
    {
        get { return Orderlines.Sum(x => (x.Quantity*x.Price)/(x.Vat + 1)); }
    }
    public decimal Mva
    {
        get { return Total - TotalExMva; }
    }

}

public partial class Orderline
{
    public decimal Total
    {
        get { return (Price*Quantity); }         
    }

    public string ProductName
    {

        get
        {
            var currentProductId = ProductID;
            return new Products().Get(currentProductId).Name;
        }
    }
}

EDIT 3 - orderline and product classes

public partial class Orderline
{
    public int ID { get; set; }
    public int ProductID { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal Vat { get; set; }
    public int OrderID { get; set; }

    public virtual Order Order { get; set; }
    public virtual Product Product { get; set; }
}
public partial class Product
{
    public Product()
    {
        this.Orderlines = new HashSet<Orderline>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string GTIN { get; set; }
    public decimal Price { get; set; }
    public decimal Vat { get; set; }

    public virtual ICollection<Orderline> Orderlines { get; set; }
}

Orderlines -> Orderline object at exception

1
This normally happens if an NavigationProperty which is (in sql)marked with ReferentialIntegrity has no value What does your order object look like? - TGlatzer
@Grumbler85 I have edited my post with some additional data. As to the ReferentialIntegrity I will read up on that. - espvar
I would check SettlementID and/or UserID - null values allowed? relation to? - Pilgerstorfer Franz
SettlementID and UserID are forreignkeys and has allow null set. - espvar
What about the orderline object (I saw count=1)? Does this orderline Object refer to the shown order object? - Pilgerstorfer Franz

1 Answers