首页 > 解决方案 > ASP.NET MVC 5 中标识表和其他表之间的关系

问题描述

在 ASP.NET MVC 5 项目中,数据库和身份表首先由代码创建,我在这个数据库中通过 SQL(不是代码优先)创建了其他表,我想通过用户 ID 将用户表与某些表连接起来。

假设名为 Qwerty 的数据库和身份表是:

  dbo.Users
  dbo.Roles
  dbo.UserClaims
  ... ect

我想通过 SQL 创建表,如下所示:

Create Table Topic.Topic
(
  TopicID int Primary key identity(1,1) not null,
  TopicAddress nvarchar(255) not null
)

Create Table dbo.Bookmark
(
  BookmarkID int Primary key identity(1,1) not null,
  BookmarkDate datetime default getdate() not null,
  UserID int constraint FK_Favorites_Users_UserID foreign key (UserID) references Users(UserID) not null
)

主题表创建成功,但是当我为书签表运行 SQL 代码时,它给我错误并用红线标记用户(表名)字

标签: c#sqlsql-serverasp.net-mvcasp.net-identity

解决方案


默认情况下,用户的主键是 nvarchar,因此,您的外键应该使用该类型定义。

Create Table dbo.Bookmark
(
  BookmarkID int Primary key identity(1,1) not null,
  BookmarkDate datetime default getdate() not null,
  UserID [nvarchar](128) constraint FK_Favorites_Users_UserID foreign key (UserID) references Users(UserID) not null
)

推荐阅读