Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迁移时出错-已存在名为';AspNetRoles';在数据库中。(实体框架核心)_C#_Asp.net Identity_Entity Framework Core - Fatal编程技术网

C# 迁移时出错-已存在名为';AspNetRoles';在数据库中。(实体框架核心)

C# 迁移时出错-已存在名为';AspNetRoles';在数据库中。(实体框架核心),c#,asp.net-identity,entity-framework-core,C#,Asp.net Identity,Entity Framework Core,我正在尝试使用ASP.NET Identity Core添加自定义配置文件详细信息。我已尝试按如下方式扩展IdentityUser类: public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } /

我正在尝试使用ASP.NET Identity Core添加自定义配置文件详细信息。我已尝试按如下方式扩展
IdentityUser
类:

public class ApplicationUser : IdentityUser
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
}
    // ===== Add Identity ========
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationIdentityDbContext>()
        .AddDefaultTokenProviders();

这是我的ApplicationDbContext,它继承了IdentityDbContext:

public class ApplicationIdentityDbContext : IdentityDbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=TokenDatabase;Trusted_Connection=True;");
    }
}
public class ApplicationIdentityDbContext : IdentityDbContext<ApplicationUser>

这是我的
startup.cs
文件:

public class Startup
{
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // ===== Add our DbContext ========
            services.AddDbContext<ApplicationIdentityDbContext>();

            // ===== Add Identity ========
            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationIdentityDbContext>()
                .AddDefaultTokenProviders();



            // ===== Add Jwt Authentication ========
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims

            services
                .AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

                })
                .AddJwtBearer(cfg =>
                {
                    cfg.RequireHttpsMetadata = false;
                    cfg.SaveToken = true;
                    cfg.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidIssuer = Configuration["JwtIssuer"],
                        ValidAudience = Configuration["JwtIssuer"],
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
                        ClockSkew = TimeSpan.Zero // remove delay of token when expire
                    };
                });

            services.AddSwaggerGen(x =>
            {
                x.SwaggerDoc("v1", new Info
                {
                    Title = "ASP NET .Identity Core Example"
                });

                var xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory + @"WebApiJwt.xml";
                x.IncludeXmlComments(xmlFilePath);
            });


            // ===== Add MVC ========
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app, 
            IHostingEnvironment env,
            ApplicationIdentityDbContext dbContext
        )
        {
            // ===== Create tables ======
            dbContext.Database.Migrate();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }


            app.UseSwagger();
            app.UseSwaggerUI(x =>
            {
                x.SwaggerEndpoint("/swagger/v1/swagger.json", "Core API");
            });

            // ===== Use Authentication ======
            app.UseAuthentication();
            app.UseMvc();         
        }
   }
类型也为
ApplicationUser
类型,但是
AspNetUsers
表没有更新这些额外的列。


简单地看了一遍,我注意到的第一件事是,您应该使用通用的IdentityDbContext:

public class ApplicationIdentityDbContext : IdentityDbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=TokenDatabase;Trusted_Connection=True;");
    }
}
public class ApplicationIdentityDbContext : IdentityDbContext<ApplicationUser>
公共类ApplicationIdentityDbContext:IdentityDbContext


简要浏览了一遍,我注意到的第一件事是您应该使用通用标识YDBContext:

public class ApplicationIdentityDbContext : IdentityDbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=TokenDatabase;Trusted_Connection=True;");
    }
}
public class ApplicationIdentityDbContext : IdentityDbContext<ApplicationUser>
公共类ApplicationIdentityDbContext:IdentityDbContext


如果数据库已初始化,则在使用迁移注释输出
数据库时,请重新创建()

如果数据库已初始化,则在使用迁移时注释掉
database.EnsureCreated()

但是为什么在执行迁移时,
AspNetRoles
的异常已经存在?感谢您的输入,但我仍然无法解决迁移问题。但是为什么在执行迁移时,
AspNetRoles
的异常已经存在?感谢您的输入,但我仍然无法解决迁移问题。