【问题标题】:Ambiguous reference for IdentityUserIdentityUser 的模糊参考
【发布时间】:2019-03-22 09:23:49
【问题描述】:

我在 -net Core 2 api 中实现身份验证,我在这段代码之后发现了这个错误:

StartUp.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<MyContext>()
            .AddDefaultTokenProviders();

MyContext.cs:

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class MyContext: IdentityDbContext<IdentityUser>
{
    public MyContext(DbContextOptions<MyContext> opt)
        : base(opt) { }

    public DbSet<Room> Rooms{ get; set; }
}

MyCONtext.cs 中的错误:

'IdentityUser' is an ambiguous reference between 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser' and 'Microsoft.AspNetCore.Identity.IdentityUser'

谢谢大家。

【问题讨论】:

  • 使用 Microsoft.AspNetCore.Identity 删除;在 MyContext.cs 中

标签: c# entity-framework .net-core


【解决方案1】:

由于IdentityUser 存在于上述两个命名空间中,您需要删除其中一个或通过using 明确指定要使用的命名空间,如下所示:

using IdentityUser = Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser

【讨论】:

  • 嗨 Rad,如果我这样做,我会收到错误消息:ApplicationUser 类型不能用作泛型类型或方法 'IdentityDbContext' 中的类型参数 'TUser' 感谢您的帮助跨度>
  • 我认为这是一个新错误,与之前的错误无关。检查这是否有帮助:stackoverflow.com/questions/49788816/…
  • 我认为主要问题是我在不同的项目中有启动和上下文,当我尝试通过 nugget 安装 identity.EntityFrameworkCore 时出现错误:检测到包降级:Microsoft.AspNetCore。 Identity.EntityFrameworkCore de 2.2.0 a 1.1.6。 WebAPI -> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 2.2.0 && Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 1.1.6)
  • 我的意思是在主项目中安装
【解决方案2】:

这可能会有所帮助

这个问题发生在我身上,就我而言,我发现问题发生了 因为旧的 NuGet 包

我安装了Microsoft.AspNetCore.Identity.EntityFrameworkCore : v 1.1.0

但当前版本是3.1.4,所以我只是更新它,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2010-09-08
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    相关资源
    最近更新 更多