【问题标题】:Relationship between Identity tables and Other tables in ASP.NET MVC 5ASP.NET MVC 5 中标识表和其他表之间的关系
【发布时间】:2019-06-17 10:18:47
【问题描述】:

在 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
)

Topic 表创建成功,但是当我为 Bookmark 表运行 SQL 代码时,它给了我错误并用 red 标记 Users(表名)字行

【问题讨论】:

  • 您确定用户 ID 是 INT 吗?默认为 varchar

标签: c# sql sql-server asp.net-mvc asp.net-identity


【解决方案1】:

默认情况下,用户的主键是 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
)

【讨论】:

  • 谢谢,我用int代替nvarchar
猜你喜欢
  • 2021-03-26
  • 2010-09-26
  • 1970-01-01
  • 2019-06-06
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
相关资源
最近更新 更多