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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:49:59+00:00 2026-06-17T04:49:59+00:00

I have User which may have zero or one Profile . From the code

  • 0

I have User which may have zero or one Profile.
From the code perspective it should be more convenient if I can access Profile from User (because I will access User information more often then its profile), but this conflicts with EF Code First:

class User
{
    public int Id { get; set; }
    [ForeignKey("Id")]
    public virtual Profile Profile { get; set; }
}
class Profile
{
    public int Id { get; set; }
}

This, put in DbContext, will create tables, where I cannot create User without having Profile created before => bad, as the User is obvious principle.
The easiest (for stupid I) solution is obvious:

class User
{
    public int Id { get; set; }
}
class Profile
{
    public int Id { get; set; }
    [ForeignKey("Id")]
    public virtual User User { get; set; }
}

This gives me more expected behaviour of databases.

But… I have feeling that this solution is not best approach. For example I’m not able to access all Users through all Profiles using lazy loading.
So the question is “How to properly setup one-to-zero/one relationship (via DataAnnotation and FluentAPI)?”
In other words “How to properly setup “one User – to – zero/one Profile” relationship?”

Maybe I think of it totally wrong…

  • 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-06-17T04:50:00+00:00Added an answer on June 17, 2026 at 4:50 am

    Thanks to Drauka. His proposal led me to the solution:

    class User
    {
        public int Id { get; set; }
        [ForeignKey("Id")]
        public virtual Profile Profile { get; set; }
    }
    class Profile
    {
        public int Id { get; set; }
        [Required, ForeignKey("Id")]
        public virtual User User { get; set; }
    }
    

    So the trick is done by Required which marks that the entity, where it is defined, is dependent while the other one is principal. And it saves one collumn in both tables (for foreign key id).

    Just one problem remained: how to ensure Profile is deleted in cascade when deleting User?

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

Sidebar

Related Questions

I have a list of user identifiers which are pretty long. The identifiers may
I have a user control which uses objects as inner properties (some code is
I have a User class which may or may not have an associated Department.
I have a databound DataGridView in a Win Forms app which the user may
I have some code that can spawn background worker threads from anywhere within it.
I have an array of sample user string inputs which may or may not
I have one Calendar object which is as per the user's time zone which
I have a list of UserProfile which may (or may not) have one or
I have a page which may has about more than 1 form like below.
I'm trying to take the user input, which may or may not have a

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.