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

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

问题描述

当我查看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
{
}

这将导致当我创建实体框架时迁移键类型为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(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?

有一个

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.

推荐答案

您需要从IdentityUser<TKey>继承自定义ApplicationUser和从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<ApplicationUser, Role, TKey>,并使用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()");
        });
    }
}

然后在Startup中将身份服务添加到这样的容器中

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的ID/主键使用Guid而不是String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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