MrMustafa
MrMustafa

Reputation: 305

Relationship between Identity tables and Other tables in ASP.NET MVC 5

In ASP.NET MVC 5 project, the database and identity tables are created by code first, and i created other tables by SQL (not by code first) in this database, and i want to join User table with some table by user Id.

Say database called Qwerty and the identity tables are:

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

I want to create tables by SQL like this:

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 table is created successfully, but when i run SQL code for Bookmark table, it give me error and mark Users (table name) word with red line

Upvotes: 0

Views: 469

Answers (1)

Felix Cen
Felix Cen

Reputation: 763

By default the primary key of Users is nvarchar, so, your foreign key should be defined with that type.

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
)

Upvotes: 1

Related Questions