IdentityUser的参考不明确

时间:2019-03-22 09:23:50

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

我正在-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'

谢谢大家。

2 个答案:

答案 0 :(得分:0)

由于IdentityUser存在于上述两个命名空间中,因此您需要删除其中之一,或者像下面这样明确指定using使用哪个命名空间:

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

答案 1 :(得分:0)

这可能有帮助

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

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

但是当前版本为3.1.4,因此我只对其进行了更新,一切正常。

相关问题