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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:53:52+00:00 2026-05-16T15:53:52+00:00

How to declare a one to one relationship using Entity Framework 4 Code First

  • 0

How to declare a one to one relationship using Entity Framework 4 Code First (POCO)?

I found this question (one-to-one relationships in Entity Framework 4) , but the article that the answer references was not useful (there is one line of code that is a 1-1 relationship, but no mention of how to define it).

  • 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-16T15:53:53+00:00Added an answer on May 16, 2026 at 3:53 pm

    Are you just looking for something like this?

    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public Profile Profile { get; set; }
        public int ProfileId { get; set; }
    }
    
    public class Profile
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string PostalCode { get; set; }
        // etc...
    }
    
    public class UserMapping : EntityConfiguration<User>
    {
        public UserMapping()
        {
            this.HasKey(u => u.Id);
            this.Property(u => u.Username).HasMaxLength(32);
    
            // User has ONE profile.
            this.HasRequired(u => u.Profile);
        }
    }
    
    public class ProfileMapping : EntityConfiguration<Profile>
    {
        public ProfileMapping()
        {
            this.HasKey(p => p.Id);
            this.Property(p => p.FirstName).HasMaxLength(32);
            this.Property(p => p.LastName).HasMaxLength(32);
            this.Property(p => p.PostalCode).HasMaxLength(6);
        }
    }
    

    EDIT: Yeah I didn’t have VS in front of me but you need to add the following line in the UserMapping instead of the current HasRequired and also add a ProfileId property (instead of Profile_Id that you added):

    this.HasRequired(u => u.Profile).HasConstraint((u, p) => u.ProfileId == p.Id);
    

    I currently don’t think there’s a way around this, but I’m sure it’ll change since we’re only in CTP4. It’d be nice if I could say:

    this.HasRequired(u => u.Profile).WithSingle().Map(
        new StoreForeignKeyName("ProfileId"));
    

    This way I wouldn’t have to include a ProfileId property. Maybe there is a way around this currently and it’s still to early in the morning for me to think :).

    Also remember to call .Include("Profile") if you want to include a “navigational property”.

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

Sidebar

Related Questions

I want to declare an entity which has several one-to-many relationships. Unfortunately, I have
Is it possible to declare more than one variable using a with statement in
I've been using EF4 (not code-first) since a year, so I'm not really an
You create a bidirectional one-to-one relationship using fields on both classes, with an annotation
I'm using a private SDK I can only declare one const global variable called
JavaScript libaries such as RequireJS and the like can be used to declare one
I want to declare three properties in my MSBuild file and overwrite one property
i have one xml like, Declare @xmldata xml; set @xmldata =' <Customers> <Id>1</Id> <Name>foo</Name>
How to merge columns into one table? declare @map_old table(ID int not null) insert
Is there any mechanism in C# that allows one to define and declare an

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.