简体   繁体   English

将标识列添加到 AspNetUsers 表 MVC Core

[英]Add Identity column to AspNetUsers table MVC Core

I am new to MVC Core.我是 MVC 核心的新手。 I am using MS Identity for user accounts.我正在为用户帐户使用 MS Identity。

I have extended the IdentityUser with my own ApplicationUser like this:我已经用我自己的 ApplicationUser 扩展了 IdentityUser,如下所示:

using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BeeKeeper.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string BusinessName { get; set; }

        public string PostalAddress { get; set; }

        [StringLength(20)]
        public string PostalPostcode { get; set; }

        public string ResidentialAddress { get; set; }

        [StringLength(20)]
        public string ResidentialPostcode { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int User_ID { get; set; }
    }
}

I need my MVC Core system to allow there to be an ID column in the AspNetUsers table that is an auto incremented column.我需要我的 MVC 核心系统允许在 AspNetUsers 表中有一个 ID 列,它是一个自动递增的列。

My client currently is using an Excel spreadsheet to register bee keepers.我的客户目前正在使用 Excel 电子表格来注册养蜂人。 They want to retain the registration numbers of those records in the spreadsheet in this new MVC system.他们想在这个新的 MVC 系统的电子表格中保留这些记录的注册号。 They get the bee keepers to label their bee hives with their registration number, which is a simple integer. So I will need to create new users in the database by importing them into the AspNetUsers table, retaining those registration numbers.他们让养蜂人到 label 他们的蜂箱和他们的注册号,这是一个简单的 integer。所以我需要通过将新用户导入到 AspNetUsers 表中来在数据库中创建新用户,并保留这些注册号。 And new users will need an auto-incremented registration number to put on their bee hive. Using an int Identity column gives me what I want but it is causing problems.新用户将需要一个自动递增的注册号来输入他们的蜜蜂 hive。使用 int Identity 列给了我想要的东西,但它会导致问题。

So I created this User_ID column that is an int identity column.所以我创建了这个 User_ID 列,它是一个 int 标识列。 The existing Id column in SpNetUsers table is not set as an identity column. SpNetUsers 表中现有的 Id 列未设置为标识列。 The Id column is nvarchar(450) with Guids in it. Id 列是 nvarchar(450),其中包含 Guid。 So I should be able to create my own identity column shouldn't I?所以我应该能够创建自己的身份列,不是吗? My understanding is that you can only have one identity column per table.我的理解是每个表只能有一个标识列。

I can go through the registration screen and submit and it creates the record in the database.我可以通过注册屏幕 go 提交并在数据库中创建记录。 The record has the correctly incremented number in the User_ID column.该记录在 User_ID 列中具有正确递增的数字。 I get sent the confirmation email with the link to click to confirm my email. But it then fails with this error when I click the link to load the ConfirmEmail page: SqlException: Cannot update identity column 'User_ID'.我收到确认邮件 email,其中包含单击以确认我的 email 的链接。但是当我单击链接以加载 ConfirmEmail 页面时,它会失败并显示此错误:SqlException:无法更新身份列“User_ID”。

This is the line of code it fails on:这是它失败的代码行:

var result = await _userManager.ConfirmEmailAsync(user, code); 

Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__169_0(Task<SqlDataReader> result)
DbUpdateException: An error occurred while updating the entries. See the inner exception for details.

Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot update identity column 'User_ID'.

   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__169_0(Task`1 result)

   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()

   at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)

   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

--- End of stack trace from previous location ---

   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

--- End of stack trace from previous location ---

   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

ClientConnectionId:58b1dd83-c877-4fc0-9563-824f3aa5e418

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.

 ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot update identity column 'User_ID'.

   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__169_0(Task`1 result)

   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()

   at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)

   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

--- End of stack trace from previous location ---

   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

--- End of stack trace from previous location ---

   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

ClientConnectionId:58b1dd83-c877-4fc0-9563-824f3aa5e418

Error Number:8102,State:1,Class:16

   --- End of inner exception stack trace ---

   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

   at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore`6.UpdateAsync(TUser user, CancellationToken cancellationToken)

   at Microsoft.AspNetCore.Identity.UserManager`1.UpdateUserAsync(TUser user)

   at Microsoft.AspNetCore.Identity.UserManager`1.ConfirmEmailAsync(TUser user, String token)

   at BeeKeeper.Areas.Identity.Pages.Account.ConfirmEmailModel.OnGetAsync(String userId, String code) in D:\mywork\BeeKeeper\Code\Areas\Identity\Pages\Account\ConfirmEmail.cshtml.cs:line 42

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object[] arguments)

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)

   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()

   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)

   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)

   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)

   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)

   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)

   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

That error is in the scaffolded ConfirmEmail.shtml.cs file which has this code: I haven't edited the code in that file apart from changing IdentityUser to ApplicationUser.该错误出现在具有以下代码的脚手架 ConfirmEmail.shtml.cs 文件中:除了将 IdentityUser 更改为 ApplicationUser 之外,我没有编辑该文件中的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BeeKeeper.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;

namespace BeeKeeper.Areas.Identity.Pages.Account
{
    [AllowAnonymous]
    public class ConfirmEmailModel : PageModel
    {
        private readonly UserManager<ApplicationUser> _userManager;

        public ConfirmEmailModel(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }

        [TempData]
        public string StatusMessage { get; set; }

        public async Task<IActionResult> OnGetAsync(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return RedirectToPage("/Index");
            }

            var user = await _userManager.FindByIdAsync(userId);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{userId}'.");
            }

            code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
            var result = await _userManager.ConfirmEmailAsync(user, code);
            StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";
            return Page();
        }
    }
}

I solved this issue by creating a new UserProfile model so that all the extra data can get saved into a new UserProfile table instead of into the AspNetUssers table.我通过创建一个新的 UserProfile model 解决了这个问题,这样所有额外的数据都可以保存到一个新的 UserProfile 表中,而不是保存到 AspNetUssers 表中。 On the Register.cshtml page I still have all the same form fields, but in the Register.cshtml.cs I now leave the Identity stuff as the default and save the extra fields into the new UserProfile table.在 Register.cshtml 页面上,我仍然拥有所有相同的表单字段,但在 Register.cshtml.cs 中,我现在将 Identity 内容保留为默认值,并将额外的字段保存到新的 UserProfile 表中。 CreatedById is the foreign key - AspNetUsers GUID of the person who created it: CreatedById 是外键 - 创建它的人的 AspNetUsers GUID:

var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };

            var result = await _userManager.CreateAsync(user, Input.Password);
            if (result.Succeeded)
            {
                // try to create the UserProfile record now that the Uer has been created:
                using (var context = new ApplicationDbContext())
                {
                    var userProfile = new UserProfile
                    {
                        FirstName = Input.FirstName,
                        LastName = Input.LastName,
                        BusinessName = Input.BusinessName,
                        PostalAddress = Input.PostalAddress,
                        PostalPostcode = Input.PostalPostcode,
                        ResidentialAddress = Input.ResidentialAddress,
                        ResidentialPostcode = Input.ResidentialPostcode,
                        CreatedById = user.Id
                    };

                    context.UserProfile.Add(userProfile);
                    context.SaveChanges();
                }

                _logger.LogInformation("User created a new account with password.");

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                var callbackUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                    protocol: Request.Scheme);

                await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                if (_userManager.Options.SignIn.RequireConfirmedAccount)
                {
                    return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
                }
                else
                {
                    await _signInManager.SignInAsync(user, isPersistent: false);
                    return LocalRedirect(returnUrl);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dbo.AspNetUsers 表上的 ASP.NET MVC 5 核心标识 CompanyID 外键 - ASP.NET MVC 5 Core Identity CompanyID foreign key on dbo.AspNetUsers table 是否将用户设置存储在ASP.NET Core身份AspNetUsers表中 - Store User Settings in ASP.NET Core Identity AspNetUsers Table or Not ASP.NET MVC-将Identity与现有的经过修改的AspNetUsers表一起使用 - ASP.NET MVC - Using Identity with existing, modified AspNetUsers table 如何在现有AspNetUsers表中添加列 - How to add a column in an existing AspNetUsers table ASP.NET Identity 从 AspNetUsers 表中删除列 - ASP.NET Identity remove column from AspNetUsers table .NET Identity 将用户 ID 从 aspnetusers 添加到没有控制器的自定义表 - .NET Identity Add userid from aspnetusers to custom table without controller ASP Net Core MVC:如何在partialview中获取aspnetusers列 - Asp Net Core MVC: How to get Column of aspnetusers in a partialview 使用Identity将新表添加到ASP .NET Core MVC应用程序中 - Add a new table into the ASP .NET Core MVC app with Identity SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;AspNetUsers&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF 如何向User.Identity添加另一个Propertys来自身份2.2.1中的表AspNetUsers - How to Add another Propertys to User.Identity From table AspNetUsers in identity 2.2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM