"The INSERT statement conflicted with the FOREIGN KEY constraint
<p>Error message:</p>

<blockquote>
  <p>The INSERT statement conflicted with the FOREIGN KEY constraint
  "FK_UserProfile_UserLogin". The conflict occurred in database
  "ToDoDB", table "dbo.UserLogin", column 'UserLoginID'. The statement
  has been terminated.</p>
</blockquote>

<p>What could this mean?</p>

<p>I am trying to build a simple log in and profile <code>MVC5</code> web app. I created my table in SQL Express. </p>

<p>Firstly here is my model for the sign up page:</p>

<pre><code>public class UserSignUp
{
    [Key]
    public int UserLoginID { get; set; }

    //Foregin key for the login table - First name, last name, creation date, and email
    public int UserProfileID { get; set; }

    [Required(ErrorMessage = "Username is required")]
    [Display(Name = "Username")]
    public string Username { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Required(ErrorMessage = "First Name is required")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required")]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime CreationDate { get; set; }

    [Required(ErrorMessage = "Valid email is required")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
}
</code></pre>

<p>So the <code>UserLoginID</code> is the primary key from the <code>UserLogin</code> table and the <code>UserProfileID</code> is the primary from the <code>UserProfile</code> table. I set the foreign key of the <code>UserProfile</code> table to <code>UserLoginID</code> from the <code>UserLogin</code>.</p>

<p>Here is my model for creating a new user:</p>

<pre><code>public class UserProfileManager
{
    public void AddUserAccount(UserSignUp newUser)
    {
        // create database connection
        using (ToDoDBEntities db = new ToDoDBEntities())
        {
            // Collect viewmodel data
            // Here building goes by object type and not foregin key relationship
            UserLogin UL = new UserLogin();
            UL.Username = newUser.Username;
            UL.Password = newUser.Password;

            // Add the UserLogin object I just built to the database
            db.UserLogins.Add(UL);
            db.SaveChanges();

            UserProfile UP = new UserProfile();
            // establish connection to UL by establishing foreign key relationship
            UP.UserLoginID = newUser.UserLoginID;
            UP.FirstName = newUser.FirstName;
            UP.LastName = newUser.LastName;
            UP.CreationDate = newUser.CreationDate;
            UP.Email = newUser.Email;

            // Add UserProfile object to databse and save changes
            db.UserProfiles.Add(UP);
            db.SaveChanges();
        }
    }

    //Check if user is real before login is allowed
    public bool isLoginReal(string LoginName)
    {
        using (ToDoDBEntities DB = new ToDoDBEntities())
        {
            // Return the user from the DB whose login name matches the LoginName string passed in as perameter
            return DB.UserLogins.Where(o =&gt; o.Username.Equals(LoginName)).Any();
        }
    }
}
</code></pre>

<p>My <code>AddUserAccount</code> is where I think I am having issues. So I start by building the <code>UserLogin</code> object and adding and saving to the database. That seems to work out actually. But the next step where I build, add, and save the <code>UserProfile</code> object doesn't seem to work. At least the database doesn't get updated.</p>

<p>Here is the controller handling the actions:</p>

<pre><code>public class AccountController : Controller
{
    // GET: Account
    public ActionResult Index()
    {
        return View();
    }

    #region signup methods
    // Get method for signup page
    public ActionResult SignUpPage()
    {
        return View();
    }

    // Post method for signup page - post to db
    [HttpPost]
    // Pass in the UserSign up model object to be built
    public ActionResult SignUpPage(UserSignUp USUV)
    {
        // Form is filled out and then method is entered
        if (ModelState.IsValid)
        {
            // Form is filled out and database connection is established if form is valid
            UserProfileManager UPM = new UserProfileManager();

            if (!UPM.isLoginReal(USUV.Username))
            {
                // data access . adduseraccount from entity manager (where model objects are built)
                UPM.AddUserAccount(USUV);
                FormsAuthentication.SetAuthCookie(USUV.FirstName, false);
                return RedirectToAction("Welcome", "Home");
            }
            else
            {

            }
        }
        return View();
    }
    #endregion
}
</code></pre>

<p>To my (noob) eye everything looks good. The <code>SignUpPage</code> is received, then a new <code>UserSignUp</code> object is passed into the <code>Post</code> action, and entity framework object (<code>UserProfileManager</code>) is built, the form is authenticated and the user gets either redirected to the <code>Welcome</code> view or the user gets returned to the signup view. </p>

<p>Any chance someone can help me figure out what I am either missing or doing wrong? I included a picture of the database design for reference (I know even less about database then MVC).</p>

<p><a href="https://i.stack.imgur.com/IAcfk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IAcfk.jpg" alt="Database design"></a></p>