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 6744513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:34+00:00 2026-05-26T12:05:34+00:00

I have 2 Entities User and User_Profile (one to one relationship). I have linked

  • 0

I have 2 Entities User and User_Profile (one to one relationship). I have linked them as follows:

public class User
{
   [Key]   
   [ForeignKey("user_profile")]
   public int user_id {get;set;}

   public string username {get;set;}
   public string email {get;set;}

   public virtual User_Proile user_profile {get;set;}
}


public class User_Profile
{
   [Key]   
   public int user_id {get;set;}

   public string firstname {get;set;}
   public string lastname {get;set;}
}

user_id is a PK in both SQL Server’s User and User_Profile tables. It is also set as an Identity column in the User table.

When I try to insert a new record via the EFDBContext Add/SaveChanges. I get the following error: “user_id cannot be NULL in the User_Profile table” This makes perfect sense as this is a PK column. I was hoping EF would be able to take the Identity user_id from Users and Insert it into User_Profile user_id when saving.

Is this possible and if so how would I implement that?

UPDATE: Please note that I manually created the DB tables and code classes so I dont have access to StoreGeneratedPattern via the .edmx file.

  • 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-26T12:05:35+00:00Added an answer on May 26, 2026 at 12:05 pm

    I think it is necessary to configure your one-to-one relationship explicitely using Fluent API:

    public class MyContext : DbContext
    {
        // ... your DbSets
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasRequired(u => u.user_profile)
                .WithRequiredPrincipal();
        }
    }
    

    And the entity classes:

    public class User
    {
        [Key]   
        public int user_id {get;set;}
        public string username {get;set;}
        public string email {get;set;}
        public virtual User_Profile user_profile {get;set;}
    }
    
    public class User_Profile
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int user_id {get;set;}
        public string firstname {get;set;}
        public string lastname {get;set;}
    }
    

    It’s necessary to switch off DatabaseGeneratedOption from Identity to None in one of the classes. I have swapped principal and dependent as it seems that the User should be the principal. The [ForeignKey(...)] is not necessary because EF will recognize user_id in User_Profile as a FK property to User.

    A code like this…

    using (var context = new MyContext())
    {
        var user = new User();
        var userProfile = new User_Profile();
    
        user.user_profile = userProfile;
    
        context.Users.Add(user);
        context.SaveChanges();
    }
    

    …should work then as you expect and save both related entities to the database.

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

Sidebar

Related Questions

I have two entities, a User and Role with a one-to-many relationship from user
i have 2 entities: User and Role i have one class Userrole that will
Suppose I have two entities: User and UserGroup . They have a one-to-many relationship
My application has the following entities: public class User { public virtual int UserID
I have two entities User and Group: @Entity public class User implements Serializable {
I have 2 entities: User and Avatar . Each user can choice one avatar
i have two entities one called User and another called Membership which has a
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
My database contains 3 tables: User and Service entities have many-to-many relationship and are
I have 2 entities : User, Address. I have set the relationship each User

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.