asp.net 使用identityDbContext和具有强类型id的身份用户

mnowg1ta  于 2023-07-01  发布在  .NET
关注(0)|答案(1)|浏览(116)

我正试图将identityDbContext添加到我的应用程序中,其中我已将id作为user的主键,就我而言,IdentityDbContext必须获取IdentityUser或其子对象。我的问题是如何添加IdentityDbContext与IdentityUser,它具有强类型id。

public class InstagramDbContext : IdentityDbContext<User, IdentityRole<Guid>, UserId>
{
   // some code
}

InstagramDbContext has this error

public record UserId(Guid Value);

public class User : IdentityUser<UserId>
{
    public UserId UserId { get; set; } = null!;
 
}
icnyk63a

icnyk63a1#

从你的问题来看,没有太多可以继续的,什么是Instagram.Domain.Users.UserId?
但是您可以尝试使用显式(或隐式)运算符

using System;

public readonly struct Digit
{
    private readonly byte digit;

    public Digit(byte digit)
    {
        if (digit > 9)
        {
            throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine.");
        }
        this.digit = digit;
    }

    public static implicit operator byte(Digit d) => d.digit;
    public static explicit operator Digit(byte b) => new Digit(b);

    public override string ToString() => $"{digit}";
}

public static class UserDefinedConversions
{
    public static void Main()
    {
        var d = new Digit(7);

        byte number = d;
        Console.WriteLine(number);  // output: 7

        Digit digit = (Digit)number;
        Console.WriteLine(digit);  // output: 7
    }
}

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

相关问题