English 中文(简体)
在 asp.net mvc EF 中的 2个表格之间创建关系表
原标题:Create the relationship table between 2 tables in asp.net mvc EF

我创造的:

  • custom custom membership provider
  • custom role provider
  • an user model
  • an role model

It create me the 2 custom tables correctly. Now I want to create the table between Users and Roles with 2 columns: RoleId, UserId

我应该调整我的模型 教EF 创建这个关系表(用户InRole)吗?

用户模式 :

 public class User
{

    [Key]
    public int UserId { get; set; }

    [Required]
    public Int32 CompanyId { get; set; }

    [Required]
    public String UserName { get; set; }

    public String Password { get; set; }

    public String PasswordSalt { get; set; }

    public String Email { get; set; }

    public Boolean IsApproved { get; set; }

    public Boolean IsLockedOut { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime LastLoginDate { get; set; }

    public DateTime LastPasswordChangedDate { get; set; }

    public DateTime LastLockoutDate { get; set; }

}

角色模型:

public class Role
{

    [Key]
    public int RoleId { get; set; }

    [Required]
    [MaxLength(20)]
    public string Name { get; set; }

    public ICollection<string> AssignedUsers { get; set; }

}
最佳回答

如果您正在使用代码一EF, 那么您需要做的就是在“ 角色类” 和“ 反之亦然” 中添加用户集。 EF 将这一双向链接作为信号, 以在基础数据存储中创建多个到多个的关系。 概括地说, 您的班级将会增加类似内容...

public class User
{
  ...

  List<Role> Roles {get; set;}

}

public class Role
{
  ...

  List<User> Users {get; set;}
}
问题回答
public class Role
{

    [Key]
    public int RoleId { get; set; }

    [Required]
    [MaxLength(20)]
    public string Name { get; set; }

    public ICollection<User> AssignedUsers { get; set; }

}


 public class User
{

    [Key]
    public int UserId { get; set; }

    [Required]
    public Int32 CompanyId { get; set; }

    [Required]
    public String UserName { get; set; }

    public String Password { get; set; }

    public String PasswordSalt { get; set; }

    public String Email { get; set; }

    public Boolean IsApproved { get; set; }

    public Boolean IsLockedOut { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime LastLoginDate { get; set; }

    public DateTime LastPasswordChangedDate { get; set; }

    public DateTime LastLockoutDate { get; set; }

    public ICollection<Role> Roles{ get; set; }

}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签