我有一个 MS-SQL 表,如下所示。
用户表
CREATE TABLE [dbo].[Users](
[UserId] [uniqueidentifier] NOT NULL Primary Key,
[UserAccount] [nvarchar](50) NOT NULL Unique,
[Password] [nvarchar](50) NOT NULL,
[UserEmail] [nvarchar](50) NOT NULL,
[JoinDate] [datetime2](7) NOT NULL,
[LoginDate] [datetime2](7) NULL)
角色表
CREATE TABLE [dbo].[Roles](
[RoleId] [uniqueidentifier] NOT NULL Primary Key,
[RoleName] [nvarchar](50) NOT NULL Unique,
[Note] [nvarchar](50) NOT NULL,
[RegistDate] [datetime2](7) NOT NULL)
UsersInRoles 表
CREATE TABLE [dbo].[UsersInRoles](
[UserId] [uniqueidentifier] NOT NULL,
[RoleId] [uniqueidentifier] NOT NULL,
[SetDate] [datetime2](7) NOT NULL,
PRIMARY KEY CLUSTERED (
[UserId] ASC,
[RoleId] ASC)WITH (
PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[UsersInRoles] WITH CHECK ADD FOREIGN KEY([RoleId]) REFERENCES [dbo].[Roles] ([RoleId]) GO
ALTER TABLE [dbo].[UsersInRoles] WITH CHECK ADD FOREIGN KEY([UserId]) REFERENCES [dbo].[Users] ([UserId]) GO
我试图在 EF Code-First 中表达这一点。
用户实体类
public class User
{
public Guid UserId { get; set; }
public string UserAccount { get; set; }
public string Password { get; set; }
public string UserEmail { get; set; }
public DateTime JoinDate { get; set; }
public DateTime LoginDate { get; set; }
}
角色实体类
public class Role
{
public Guid RoleId { get; set; }
public string RoleName { get; set; }
public string Note { get; set; }
public DateTime RegistDate { get; set; }
}
UsersInRole 实体类
public class UsersInRole
{
public Guid UserId { get; set; }
public Guid RoleId { get; set; }
public DateTime SetDate { get; set; }
}
外键的问题,应该如何设计UsersInRoles?
如果有更好的设计,请告诉我。