0
votes

I have a Order < Customer model.

How do I set this up? When I try to insert a user with a null customerid, it tells me that it cannot be null.

The model is as follows:

public class Order
{
    public long OrderID { get; set; }
    public int CustomerID { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}

public partial class Customer
{
    public int CustomerID { get; set; }   
}

In DbContext :

modelBuilder.Entity<Customer>()
            .HasMany(e => e.Orders)
            .WithRequired(e => e.Customer)
            .WillCascadeOnDelete(false);
1
You need to make the CustomerID property nullable: public int? CustomerID { get; set; } - Felipe Cruz
Could you clarify your questions, as I don't understand "A user belongs to a country, but may not belong to any (null foreign key)". What does that mean to you? What does it have to do with the Order class you present? Please be a bit more precise with your question. Optionally, from what I can observe you can try to make the CustomerID property nullable. (edit: just like @Felipe Cruze just mentioned above :)) - Yves Schelpe
You are talking about problems with user and country but the code deals with customer and order. Did not make sense to me - Sir Rufo

1 Answers

1
votes

Have you tried to replace line

.WithRequired(e => e.Customer)

with

.WithOptional(e => e.Customer)

?

And I make the CustomerID property nullable: public int? CustomerID { get; set; }