简体   繁体   English

在 ASP.NET Identity Core 中,我可以更改 UserToken 主键吗

[英]In ASP.NET Identity Core can I change UserToken primary key

Identity generates UserToken table for me (code first). Identity 为我生成 UserToken 表(代码优先)。 I don't like that it uses 3-part primary key and there is no specified UserTokenId column.我不喜欢它使用 3 部分主键并且没有指定的 UserTokenId 列。

CREATE TABLE [UserToken] (
    [UserId] int NOT NULL,
    [LoginProvider] nvarchar(450) NOT NULL,
    [Name] nvarchar(450) NOT NULL,
    [Value] nvarchar(max),
    CONSTRAINT [PK_UserToken] PRIMARY KEY ([UserId], [LoginProvider], [Name])
);

Is it possible to change (in my C# project code) the table to use only one id column as primary key?是否可以(在我的 C# 项目代码中)更改表以仅使用一个 id 列作为主键? Or is it actually even a good practice in this case?或者在这种情况下它实际上是一个很好的做法?

I have read many instructions on how to change the id data type in the user table but it's a bit different case.我已经阅读了许多关于如何更改用户表中的 id 数据类型的说明,但情况略有不同。

You might change the table structure manually by creating an IDENTITY column and by creating a UNIQUE index on the three columns in question...您可以通过创建IDENTITY列并在相关的三列上创建 UNIQUE 索引来手动更改表结构...

But for what reason?但出于什么原因?

The identify framework will always look up data based on the three value tuple and not on a artificial primary key which does not reflect any real data.识别框架将始终基于三值元组而不是不反映任何真实数据的人工主键来查找数据。

An identity column might make sense in case this value is used as a reference on another table, in this case there might be a reason to have a simple foreign key instead of the three value tuple.如果此值用作另一个表的引用,则标识列可能有意义,在这种情况下,可能有理由使用简单的外键而不是三值元组。

If you are still not convinced to leave the table as it is :-) :如果您仍然不相信原样离开桌子:-):

If you want to customize your table/database layout that is used by ASP.NET Identity, this post might be a good starting point (Beware that this post is based on ASP.NET core identity).如果您想自定义 ASP.NET Identity 使用的表/数据库布局, 这篇文章可能是一个很好的起点(请注意,这篇文章基于 ASP.NET 核心身份)。

Background: ASP.NET is based on a layered approach where the storage used to persist information about the ASP.NET identity information is a separate module inside the ASP.NET identity framework.背景:ASP.NET 基于分层方法,其中用于保存有关 ASP.NET 标识信息的信息的存储是 ASP.NET 标识框架内的一个单独模块

You might use this as a starting point to create your own persistence layer with your own database model.您可以以此为起点,使用您自己的数据库模型创建您自己的持久层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改Asp.net身份和GetUserID的主键<int> - Change Primary Key for Asp.net Identity and GetUserID<int> ASP.NET Core 2.0身份修改从字符串到Guid的主键 - ASP.NET Core 2.0 Identity Modifying Primary Key from String to Guid 在ASP.NET Identity 2.0中将用户的主键从字符串更改为Int-麻烦吗? - Change primary key from string to Int for users in ASP.NET Identity 2.0 - Worth the trouble? 如何为 ASP.NET Identity 2 生成我自己的主键 - How to generate my own primary key for ASP.NET Identity 2 ASP.NET Core DB-first Entity Framework Core 不能自动增加主键 - ASP.NET Core DB-first Entity Framework Core can not auto increment primary key ASP.NET Identity UserID主键是自定义表中的外键和主键 - ASP.NET Identity UserID primary key to be foreign key and primary key in your custom table 无法将ID设置为ASP.NET Core代码优先EF模型中的主键 - Can not set Id as primary key in ASP.NET Core code-first EF model asp.net核心身份RoleTable的RoleId作为外键 - asp.net core identity RoleTable's RoleId as foreign key 带串联主键的ASP.NET Core 2.1编辑 - ASP.NET Core 2.1 Edit w/ Concatenated Primary Key 如何使用Asp.Net.Identity和Entity Framework 6在Asp.Net Core 2.0 Target Framework 4.6.1中设置身份? - How can I setup identity in Asp.Net Core 2.0 Target Framework 4.6.1 using Asp.Net.Identity and Entity Framework 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM