我一直在项目中使用 ADO.NET 实体数据模型来测试我一直在开发的安全/授权方法。我让一切都按我想要的方式工作,我需要在另一个生产项目中模仿这种行为。
另一个生产产品使用 Entity Framework Power Tools Beta 3 来生成类/实体和映射类。到目前为止,它只有模型和映射。
我有一个 Users 表、Roles 表和 UsersInRoles 表(等等)。
Roles 和 UsersInRoles 之间是一对多的关系,但 Users 和 UsersInRoles 之间是一对一的关系。
我认为我的数据库关系正确。ADO 模型似乎是这么认为的 :)
但是,当我在使用 Power Tools 生成的类时在我的 C# 代码中调用 db.Users 时,我得到:
{“执行命令定义时发生错误。有关详细信息,请参阅内部异常。”}
{"无效的列名 'Role_RoleId'。"}
当我执行 EF 命令 - 查看实体数据模型 DDL SQL 时,我看到一个名为 Role_RoleID 的列,这是不正确的......对吗?
数据库表 Users 中没有 Role_RoleId。此外,模型或映射文件中不会生成 UsersInRoles。我该如何解决?我很茫然。
[...]
create table [Auth].[Users] (
[UserId] [uniqueidentifier] not null,
[UserName] [nvarchar](100) not null,
[FullUserName] [nvarchar](100) not null,
[Password] [nvarchar](150) not null,
[PasswordSalt] [nvarchar](50) not null,
[IsEnabled] [bit] not null,
[IsLockedOut] [bit] not null,
[Email] [nvarchar](150) not null,
[DateCreated] [datetime] not null,
[LastModified] [datetime] not null,
[Role_RoleId] [uniqueidentifier] null,
primary key ([UserId])
);
alter table [Auth].[Users] add constraint [User_Role] foreign key ([Role_RoleId]) references [Auth].[Roles]([RoleId]);
[...]
SQL Server 表
CREATE TABLE [Auth].[Users](
[UserId] [uniqueidentifier] NOT NULL,
[UserName] [nvarchar](100) NOT NULL,
[FullUserName] [nvarchar](100) NOT NULL,
[Password] [nvarchar](150) NOT NULL,
[PasswordSalt] [nvarchar](50) NOT NULL,
[IsEnabled] [bit] NOT NULL,
[IsLockedOut] [bit] NOT NULL,
[Email] [nvarchar](150) NOT NULL,
[DateCreated] [datetime2](7) NOT NULL,
[LastModified] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[UserId] 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
CREATE TABLE [Auth].[Roles](
[RoleId] [uniqueidentifier] NOT NULL,
[RoleName] [nvarchar](100) NOT NULL,
[Description] [nvarchar](250) NULL,
CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED
(
[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
CREATE TABLE [Auth].[UsersInRoles](
[UserId] [uniqueidentifier] NOT NULL,
[RoleId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_UsersInRoles] PRIMARY KEY CLUSTERED
(
[UserId] 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 [Auth].[UsersInRoles] WITH CHECK ADD CONSTRAINT [FK_UsersInRoles_Roles] FOREIGN KEY([RoleId])
REFERENCES [Auth].[Roles] ([RoleId])
ON UPDATE CASCADE
ON DELETE SET DEFAULT
GO
ALTER TABLE [Auth].[UsersInRoles] CHECK CONSTRAINT [FK_UsersInRoles_Roles]
GO
ALTER TABLE [Auth].[UsersInRoles] WITH CHECK ADD CONSTRAINT [FK_UsersInRoles_Users] FOREIGN KEY([UserId])
REFERENCES [Auth].[Users] ([UserId])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [Auth].[UsersInRoles] CHECK CONSTRAINT [FK_UsersInRoles_Users]
GO
ALTER TABLE [Auth].[UsersInRoles] ADD CONSTRAINT [DF_UsersInRoles_RoleId] DEFAULT ('8B24673F-A4FB-4031-9185-8CCBD96CBE4D') FOR [RoleId]
GO
CS文件
public partial class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string FullUserName { get; set; }
public string Password { get; set; }
public string PasswordSalt { get; set; }
public bool IsEnabled { get; set; }
public bool IsLockedOut { get; set; }
public string Email { get; set; }
public System.DateTime DateCreated { get; set; }
public System.DateTime LastModified { get; set; }
public virtual Role Role { get; set; }
}
public partial class Role
{
public Role()
{
this.RoleSecurables = new List<RoleSecurable>();
this.Users = new List<User>();
}
public int RoleId { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public virtual ICollection<RoleSecurable> RoleSecurables { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public UserMap()
{
// Primary Key
this.HasKey(t => t.UserId);
// Properties
this.Property(t => t.UserName)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.FullUserName)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.Password)
.IsRequired()
.HasMaxLength(150);
this.Property(t => t.PasswordSalt)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.Email)
.IsRequired()
.HasMaxLength(150);
// Table & Column Mappings
this.ToTable("Users", "Auth");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.UserName).HasColumnName("UserName");
this.Property(t => t.FullUserName).HasColumnName("FullUserName");
this.Property(t => t.Password).HasColumnName("Password");
this.Property(t => t.PasswordSalt).HasColumnName("PasswordSalt");
this.Property(t => t.IsEnabled).HasColumnName("IsEnabled");
this.Property(t => t.IsLockedOut).HasColumnName("IsLockedOut");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.DateCreated).HasColumnName("DateCreated");
this.Property(t => t.LastModified).HasColumnName("LastModified");
}
public RoleMap()
{
// Primary Key
this.HasKey(t => t.RoleId);
// Properties
this.Property(t => t.RoleName)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.Description)
.HasMaxLength(250);
// Table & Column Mappings
this.ToTable("Roles", "Auth");
this.Property(t => t.RoleId).HasColumnName("RoleId");
this.Property(t => t.RoleName).HasColumnName("RoleName");
this.Property(t => t.Description).HasColumnName("Description");
}