InsertOrUpdateGraph 1 to 1

Jan 18 at 6:16 PM
Edited Jan 24 at 7:30 PM
Trying to insert graph.
public class Customer : DatabaseEntity
{
    public int Id { get; set; }
    public virtual CustomerUser CustomerUser { get; set; }
}

public class CustomerUser : DatabaseEntity
{
    [ForeignKey("Customer")]
    public int CustomerUserId { get; set; }
    public virtual Customer Customer { get; set; }
 }
...
customer.CustomerUser = customerUser
_customerService.InsertOrUpdateGraph(customer);
await _unitOfWorkAsync.SaveChangesAsync();

Get error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.CustomerUser_dbo.Customer_CustomerUserId". The conflict occurred in database "Ebpp", table "dbo.Customer", column 'Id'. The statement has been terminated.

What am I missing?
Jan 19 at 1:40 PM
Edited Jan 19 at 1:47 PM
Have you set identity?
public class Customer : DatabaseEntity
{
    public int Id { get; set; }
Is this value set to null during creation of object?
Your code seems to be look ok.
Jan 23 at 4:18 PM
Yes, identity is set. If I do this in two steps, insert the Customer, get the id and populate it to CustomerUser it works. Was trying to save a step.
Jan 24 at 7:29 PM
Got it to work by going this route
            using (var context = new Context())
            {
                context.Customers.Add(customer);
                context.SaveChanges();
                return Ok(customer);
            }
Marked as answer by TheFinalSQL on 1/24/2017 at 12:29 PM