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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:42:35+00:00 2026-06-11T15:42:35+00:00

Let’s assume I have these tables: Role – RoleId – Name UserRole – UserId

  • 0

Let’s assume I have these tables:

Role
- RoleId
- Name

UserRole
- UserId
- RoleId

User
- UserId
- Username

UserEmail
- UserId
- EmailId
- IsPrimary

Email
- EmailId
- Address

Now my model, should look something like this:

public class Role {
    public int RoleId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

public class User {
    public int UserId { get; set; }
    public string Username { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual ICollection<UserEmail> UserEmails { get; set; }
}

public class UserEmail {
    public int UserId { get; set; }
    public int EmailId { get; set; }
    public bool IsPrimary { get; set; }
    public virtual User User { get; set; }
    public virtual Email Email { get; set; }
}

public class Email {
    public int EmailId { get; set; }
    public string Address { get; set; }
    public virtual ICollection<UserEmail> UserEmails { get; set; }
}

These are common and specific things I would like to do in this particular case:

Add the primary keys:

modelBuilder.Entity<Role>().HasKey(q => q.RoleId);

Will this have the same effect than using the Key attribute in the entity property? If so, why bother using the modelBuilder when using Data Annotations is shorter and easier to read/write? Is there any convention on when to use Data Annotations or Fluent API?

Add many to many relationships:

modelBuilder.Entity<Role>()
    .HasMany(q => q.Users)
    .WithMany(q => q.Roles)
    .Map(q => {
                  q.MapLeftKey("RoleId");
                  q.MapRightKey("UserId");
                  q.ToTable("UserRoles");
              });

Add one to many relationships:

modelBuilder.Entity<UserEmail>()
    .HasRequired(q => q.User)
    .WithMany(q => q.UserEmails)
    .HasForeignKey(q => q.EmailId);

Is the last line required?

  • 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-11T15:42:37+00:00Added an answer on June 11, 2026 at 3:42 pm

    Yes, that HasKey method has the same effect as the [Key] attribute. You might have a lot of configuration to do and prefer to keep it all in the ModelBuilder method. On the other hand you might have very little or prefer to use the attributes for some of it. Just gives you some flexibility.

    If you’re talking about the:

    .HasForeignKey(q => q.EmailId);

    Then in your case, yes it is required. Why? Because you’ve created your own foreign key property in the UserEmail entity. If you deleted the property you could remove this line and EF would create one for you in the database called Email_Id. You could still access this through the navigation property instanceOfUserEmail.Email.EmailId.

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

Sidebar

Related Questions

Let's say I have a method in java, which looks up a user in
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have an facebook application running using the JS SDK. First user
Let's assume that a user votes for some movies in a scale of 1
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let assume we have two activities. A - main activity, that is home launcher
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a user enter the URL of an image. After URL
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012

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.