如何让 EF-Core 使用 Guid 而不是 String 作为其 ID/主键 [英] How to make EF-Core use a Guid instead of String for its ID/Primary key

查看:47
本文介绍了如何让 EF-Core 使用 Guid 而不是 String 作为其 ID/主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看 ASP.NET 3 Identity 时,它使用 string 而不是 Guid 作为唯一主键.

When I look at the ASP.NET 3 Identity it uses a string and not a Guid for the unique primary key.

在我的Entity Framework code first 用户的ApplicationUser 类中,我继承了Identity 类

In my Entity Framework code first Users' ApplicationUser class I inherit the Identity class

public class ApplicationUser : IdentityUser
{
}

这导致当我创建我的实体框架迁移表 aspnetusers 使用 nvarchar(450) 而不是 uniqueidentifier 的键类型创建>

which results in when I create my Entity Framework migrations a table aspnetusers getting created with a key type ofnvarchar(450) instead of uniqueidentifier

当我将其与 ASP.NET Identity 2 ENtity Framework 项目进行比较时,它创建了一个 uniqueidentifier 而不是 nvarchar 的 ID 字段(450)

When I compare this to ASP.NET Identity 2 ENtity Framework project it created an ID field of uniqueidentifier and not nvarchar(450)

我认为 uniqueidentifier 的数据库性能主键和外键会比 nvarchar(450)

I would imagine for database performance primary keys and foreign keys of uniqueidentifier would be better than nvarchar(450)

有没有办法用Guid代替stringuniqueidentifier代替nvarchar(450)ASP.NET Identity 3?

Is there a way to use for the unique key Guid instead of string and uniqueidentifier instead of nvarchar(450) with ASP.NET Identity 3?

有一个 上一个问题 如何将字符串转换为 Guid,但我希望数据库表 Id 为 Guid.

There is a previous question how to convert a String to a Guid but I want the database table Id to be a Guid.

我发现了另一个 问题 以及当长度为 nvarchar(128) 时之前的 BETA.给出的原因是并非所有数据库都支持 Guid,并且为了灵活性而对其进行了更改.

I found another question as well for a previous BETA when the length was nvarchar(128). The reason given is that not all databases support Guids and it was changed for flexibility.

是否必须有一种简单的方法可以在不重写整个 identity 3 的情况下将字符串更改为 Guid?

Is there must be an easy way to change from string to Guid without rewriting the whole identity 3?

nvarchar(450) 真的是矫枉过正,在创建 SQL Server 数据库约束时会给出各种警告.数据库管理员绝对不会喜欢这些警告.

nvarchar(450) is really overkill and gives all kinds of warnings when creating SQL Server database constraints. Database admins will definitively not like these warnings.

推荐答案

你需要自定义ApplicationUser继承自IdentityUser和自定义Role继承自IdentityRole<;TKey>

You need custom ApplicationUser inherit from IdentityUser<TKey> and custom Role inherit from IdentityRole<TKey>

public class ApplicationUser : IdentityUser<Guid> { }    
public class Role : IdentityRole<Guid> { }

自定义上下文类继承自 IdentityDbContext 并使用 fluent api 自动生成 guid 密钥.

Custom context class inherit from IdentityDbContext<ApplicationUser, Role, TKey> and use fluent api for auto generate guid keys.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });

        builder.Entity<Role>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
        });
    }
}

然后在启动时将身份服务添加到容器中

then in Startup add Identity service to container like this

services.AddIdentity<ApplicationUser, Role>()
        .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
        .AddDefaultTokenProviders()
        .AddUserStore<UserStore<ApplicationUser, Role, ApplicationDbContext, Guid>> ()
        .AddRoleStore<RoleStore<Role, ApplicationDbContext, Guid>>();

如果您还没有创建数据库,请清除迁移文件夹并运行 ef 命令

If you have not created the database, clear the migrations folder and run ef commands

这篇关于如何让 EF-Core 使用 Guid 而不是 String 作为其 ID/主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆