Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4119058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:02:30+00:00 2026-05-20T23:02:30+00:00

Currently I have my entities working with models that look like: public class MyModel

  • 0

Currently I have my entities working with models that look like:

public class MyModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual User User { get; set; }
}

This is working good, but I keep seeing CodeFirst examples with explicitly created Id properties for foreign entities in the model. This makes sense so I don’t have to do lazy load the user to retrieve the id value of the user associated with a MyModel instance.

However, when I change MyModel to be:

public class MyModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual int UserId { get; set; }
    public virtual User User { get; set; }
}

I get the following exception when attempting to commit MyModel instances:

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "MyModel_User". The conflict occurred in database "mjlunittest", table "dbo.Users", column 'Id'.

Why am I getting this exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:02:31+00:00Added an answer on May 20, 2026 at 11:02 pm

    When you move from your first example of MyModel to the second example the EF Code-First conventions detect one important change:

    In the fist case where you don’t have a foreign key property in the model the relationship MyModel -> User is optional because the property User in MyModel can – as a reference to another object – be null. So, the EF conventions will create a nullable foreign key column in the database.

    In the second case you are telling EF that your foreign key UserId is of type int which is not nullable. Therefore EF convention rules will consider the relationship MyModel -> User as required and create an Int32 column in the database which is not nullable.

    If you create an object of the second version type MyModel, add it to the context and call SaveChanges you’ll get the exception you described – unless you have a User in the database with the foreign key (which had to be 0 if you don’t specify another value when you create the object). Creating and saving an object of the first version won’t throw this exception because here the foreign key in the database is allowed to be null.

    If you want that both examples of MyModel behave the same way you can either make the foreign key property nullable in the second example:

    public virtual int? UserId { get; set; }
    

    Then the relationship is optional in both examples of MyModel.

    Or you can make the User property required in the first example:

    [Required]
    public virtual User User { get; set; }
    

    Then the relationship is required in both examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I currently have links w/ class=ajax that I want to retrieve the element with
I currently have a UINavigationController based application that works just fine. I'd like to
After researching all day and night, I have something that is currently working. However,
Currently I have a database with the following relationships: One Client has multiple Entities
Currently have password protection on my main and sub directories, however I'd like to
I currently have a need for a custom ListViewItem class - let's call it
I am currently working on a project which has Employee, Manager entities. At the
I have a very strange error happening in an App that has been working
I'm currently facing in problem using Core Data. I have two entities A and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.