【问题标题】:Override Identity ID type in .net core [duplicate]覆盖.net核心中的身份ID类型[重复]
【发布时间】:2017-01-08 17:24:19
【问题描述】:

我正在尝试更改身份用户上默认字符串的 ID 类型。 尝试在配置服务方法中注册身份服务时出现异常。

这就是我在 Startup.ConfigureServices 方法中尝试做的事情。

public void ConfigureServices(IServiceCollection services)
{

        services.AddDbContext<UserContext>(options =>
               options.UseSqlServer(config.GetConnectionString("DefaultConnection")));

        services.AddIdentity<User, Role>()
        .AddEntityFrameworkStores<UserContext>();
      //.AddDefaultTokenProviders();

        services.AddDbContext<MusicContext>(options =>
                   options.UseSqlServer(config.GetConnectionString("DefaultConnection")));

        services.Configure<IdentityOptions>(options =>
        {
          new IdentityOptionConfiguration(options);
        });    
     services.AddMvc();   
}

这是我继承 IdentityUser 的类。这是我试图覆盖字符串类型的地方。我希望它是一个 int 甚至是一个 guid。我还不确定哪个,但我现在要使用 int。

 public class User : IdentityUser<int>
 {
    public int ID { get; set; }
 }

这是我尝试使用的角色。大多数情况下,我可以接受默认角色。

 public class Role : IdentityRole
 {
 }

现在我得到的异常如下......

System.Private.CoreLib.ni.dll 但未在用户代码中处理

附加信息:GenericArguments[0], 'MusicianProject.Services.User',在 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' 违反了“TUser”类型的约束。

根据错误的措辞,我相信我试图更改类型很疯狂。我在这里错过了什么?

【问题讨论】:

  • 示例 here 使用 Guid 进行 PK
  • @tmg:你应该为它发起一次近距离投票,所以它会被清楚地标记为重复,并且未来的用户会被指向它
  • @Tseng 你是对的

标签: c# asp.net-core ef-code-first asp.net-identity


【解决方案1】:

感谢 tmg 将我链接到类似问题。那个帖子解决了我的情况。尽管当我尝试创建迁移时,我与 guid 和 int 发生了某种操作数冲突。我会试着弄清楚。

谢谢

【讨论】:

  • 您有没有弄清楚您的迁移问题?我怀疑我可能遇到了同样的问题。
  • 我做到了。但是,我对修复它的方法一无所知。我认为是 b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");部分。摆脱它并尝试运行迁移。如果这不能解决它,请告诉我。抱歉回复晚了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多