Asp.net 向用户类添加属性违反了TContext类型的约束

Asp.net 向用户类添加属性违反了TContext类型的约束,asp.net,entity-framework,identity,entity-framework-migrations,Asp.net,Entity Framework,Identity,Entity Framework Migrations,我试图向ApplicationUser类添加属性,但在添加属性后更新数据库时遇到问题。我做错了什么?我所要做的就是将声明的结果存储在用户对象中,以便更轻松地访问它 我正在尝试使用以下命令更新数据库:EntityFrameworkCore\Add Migration-Name ApplicationDbContext 每当我运行命令时,我都会在NuGet包管理器中运行它。我还尝试过在我的项目文件夹中通过命令提示符运行命令(没有成功,也没有类似的错误输出) 版本信息: 实体框架核心和实体框架6都已

我试图向ApplicationUser类添加属性,但在添加属性后更新数据库时遇到问题。我做错了什么?我所要做的就是将声明的结果存储在用户对象中,以便更轻松地访问它

我正在尝试使用以下命令更新数据库:
EntityFrameworkCore\Add Migration-Name ApplicationDbContext

每当我运行命令时,我都会在NuGet包管理器中运行它。我还尝试过在我的项目文件夹中通过命令提示符运行命令(没有成功,也没有类似的错误输出)

版本信息:

  • 实体框架核心和实体框架6都已安装。实体框架核心工具正在运行。我用
    EntityFrameworkCore\

  • dotnet--version
    2.1.101

错误

一般性论证[0], “MyWebsite1.Data.Migrations.ApplicationDbContext”,位于 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory'1[TContext]' 违反了“TContext”类型的约束

我的ApplicationUser类:

 public class ApplicationUser : IdentityUser
    {
        public string LGID { get; } = "";
        public string CoIDs { get; } = "";
        public string LGIDSuperUserName { get; set; } = "unknown"; //recently added
        public bool IsSuperUser { get; set; } = false; //recently added

        public ApplicationUser() { }

        public ApplicationUser(ClaimsIdentity identity)
        {
            IEnumerable<Claim> claims = identity.Claims;
            foreach (Claim c in claims)
            {
                if (c.Type == "LGID")
                    LGID = c.Value;
                if (c.Type == "CoIDs")
                    CoIDs = c.Value;
                if (c.Type == "LGIDSuperUser")
                    LGIDSuperUserName = c.Value;
                if (c.Type == "IsSuperUser")
                    IsSuperUser = c.Value.ToLower()=="yes";
            }
        }
    }
应用程序上下文

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}
public class ApplicationDbContext:IdentityDbContext当我尝试时:
dotnet ef迁移添加超级用户\u测试
我得到:
dotnet:没有找到匹配命令“dotnet ef”的可执行文件
,尽管在我的.csproj中有DotNetCliToolReference并运行:
dotnet restore

我的迁移:

您的
应用程序的bContext
应该实现
IDesignTimeDbContextFactory

ApplicationDbContext
应该如下所示

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDesignTimeDbContextFactory<ApplicationDbContext>
{

    public ApplicationDbContext() { }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public ApplicationDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS; Database=TestEfCore; Trusted_Connection=True;");

        return new ApplicationDbContext(optionsBuilder.Options);
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }   
}
然后跑


您的上下文是什么样子的?见@SteveGreene我添加了我的上下文。这就是你所指的吗?可能是一些依赖性问题。什么是EF版本,等等。请看Ivan的答案添加了我的EF版本,这里还有其他有用的信息吗?我不熟悉迁移和实体框架尝试:
EntityFrameworkCore\Add Migration-Name test1
,但得到了相同的错误:(您是否删除了migrations文件夹下的旧类
ApplicationDbContext
?我在帖子中添加了migrations文件夹的屏幕截图,我需要删除哪个文件?如果您的数据库中没有关键数据,请删除migration文件夹下的所有文件,如果存在,请删除数据库。我只需要索赔表和如果我删除迁移中的所有文件,会影响这些文件吗?
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <_SelectedScaffolderID>MvcControllerWithContextScaffolder</_SelectedScaffolderID>
    <_SelectedScaffolderCategoryPath>root/Common</_SelectedScaffolderCategoryPath>
    <WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
    <WebStackScaffolding_DbContextDialogWidth>600</WebStackScaffolding_DbContextDialogWidth>
    <WebStackScaffolding_ViewDialogWidth>600</WebStackScaffolding_ViewDialogWidth>
    <WebStackScaffolding_IsLayoutPageSelected>False</WebStackScaffolding_IsLayoutPageSelected>
    <WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
    <WebStackScaffolding_IsReferencingScriptLibrariesSelected>False</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
    <ActiveDebugProfile>IIS Express</ActiveDebugProfile>
    <NameOfLastUsedPublishProfile>FolderProfile</NameOfLastUsedPublishProfile>
    <Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
    <Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
    <WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
  </PropertyGroup>
<DotNetCliToolReference   
Include="Microsoft.EntityFrameworkCore.Tools.DotNet"
Version=”1.0.0” />
</Project>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDesignTimeDbContextFactory<ApplicationDbContext>
{

    public ApplicationDbContext() { }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public ApplicationDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS; Database=TestEfCore; Trusted_Connection=True;");

        return new ApplicationDbContext(optionsBuilder.Options);
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }   
}
<ItemGroup>
  <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0-preview1-final" />
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview1-final" />
</ItemGroup>
dotnet restore
dotnet ef