How to map a lookup table using Entity Framework code First with the Fluent API

I am new to asp.net mvc and in the root code of entity code, I am not interested in databases either. I apologize in advance for the wrong terminology or the way I understand things.

Now to the question. I have the following models:
User model

public class User { public int UserId { get; set; } public string Username { get; set; } public string Password { get; set; } public int RoleId { get; set; } [ForeignKey("RoleId")] public virtual IEnumerable<Role> Roles { get; set; } } 

Role model

  public class Role { public int RoleId { get; set; } public string RoleName { get; set; } } 

Ultimately, I want to use the Ef codefirst method with a white API to map UserId and RoleId to a User_Role table with a one-to-many relationship, the user can have several roles: User_Role lookup table

I assume that what has been done on this issue is the correct approach, except that the author has used the many-to-many combination. I tried it this way, but the part with u => u.users gives me an error (I assume it is because the model does not have a user property, so he answered his question, but did not update his question?)

My question is: What is the exact free api code allowing Ef to generate this table for me?

Things I'm Not Sure About: (Feel free to ignore)

  • Is this the right approach to my problem?
  • Once I have a lookup table, is this still the right way to declare my navigation property so that I can later use it as user.Roles and restore their roles?
  • where will the RoleId value be displayed in the User model from the role table or User_Role?
  • Should I use an identifier in the lookup table?

Thanks in advance! I really appreciate your experience.

+8
database asp.net-mvc-4 ef-code-first fluent
source share
1 answer

First you must get rid of the RoleId property in the User model. Having this as a foreign key, it is indicated what role the user plays. Since the user can have many roles, the foreign key should not be in the user table, but in the mapping table.

So, you have a many-to-many relationship between users and roles, and the Entity Framework can automatically create the necessary mapping table without the need to configure anything.

If you have the Roles property in the User object and the Users property in the Role object, EF will find out that you want to have many-to-many between them, and create a table with the primary keys of both objects as a combined primary key that will be used for Mapping users to roles.

When loading User from the database, you can use the Roles navigation property to find out what roles the user has, and you can load Role to find out which users are in that role.

The easiest way to do this would be something like this:

 public class Context : DbContext { public DbSet<User> Users { get; set; } public DbSet<Role> Roles { get; set; } static Context() { Database.SetInitializer(new DropCreateDatabaseAlways<Context>()); } public Context() : base("Server=localhost;Initial Catalog=Test;Integrated Security=True;") { } } public class User { public int UserId { get; set; } public string Username { get; set; } public string Password { get; set; } public List<Role> Roles { get; set; } } public class Role { public int RoleId { get; set; } public string RoleName { get; set; } public List<User> Users { get; set; } } 

Running this code leads to the following 3 tables:

enter image description here

+22
source share

All Articles