将guid.newguid添加到sql数据库问题

0lvr5msh  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(341)

在数据库的users表中添加新记录时遇到问题。我将id作为uniqueidentifier,并尝试使用guid.newguid()作为此id从c添加新记录。
sql返回错误,该id不能为空。我尝试在数据库中添加带有这个生成的guid id的记录,我看到了相同的错误,但是如果我要使用sqlnewid添加记录,我会看到该记录已成功添加。
我已经用ef的代码创建了数据库。项目是在asp.net mvc 5中创建的。
有人能帮我吗?
sql表:

CREATE TABLE [dbo].[Users](
[Id] [uniqueidentifier] NOT NULL,
[Email] [nvarchar](256) NULL,
[PasswordHash] [nvarchar](max) NULL,
[SecurityStamp] [nvarchar](max) NULL,
[LockoutEndDateUtc] [datetime] NULL,
[UserName] [nvarchar](256) NOT NULL,
 CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

c代码:

public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public override Guid Id { get; set; }

    public User()
    {
        Id = Guid.NewGuid();
    }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class UserLogin : IdentityUserLogin<Guid>
{ }

public class MyRole : IdentityRole<Guid, UserRole>
{
}

public class UserRole : IdentityUserRole<Guid>
{
}

public class UserClaim : IdentityUserClaim<Guid>
{ }

public class CustomUserStore : UserStore<User, MyRole, Guid, UserLogin, UserRole, UserClaim>
{
    public CustomUserStore(DbContext context) : base(context)
    {
    }
}

public class CustomRoleStore : RoleStore<MyRole, Guid, UserRole>
{
    public CustomRoleStore(DbContext context) : base(context)
    {
    }
}

public class RecruitmentDbContext : IdentityDbContext<User, MyRole, Guid, UserLogin, UserRole, UserClaim>
{
    public RecruitmentDbContext()
        : base("RecruitmentDB")
    {
    }

    public static RecruitmentDbContext Create()
    {
        return new RecruitmentDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>()
            .Property(prop => prop.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<User>()
            .Ignore(col => col.PhoneNumber)
            .Ignore(col => col.EmailConfirmed)
            .Ignore(col => col.PhoneNumberConfirmed)
            .Ignore(col => col.TwoFactorEnabled)
            .Ignore(col => col.LockoutEnabled)
            .Ignore(col => col.AccessFailedCount);
        modelBuilder.Entity<User>()
            .ToTable("Users");

        modelBuilder.Entity<MyRole>()
            .ToTable("Roles");

        modelBuilder.Entity<UserRole>()
            .ToTable("UserRoles");

        modelBuilder.Entity<UserClaim>()
            .ToTable("UserClaims");

        modelBuilder.Entity<UserLogin>()
            .ToTable("UserLogins");
    }
}

以及我在c#控制器中引发错误的寄存器操作

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    try
    {

        if (ModelState.IsValid)
        {
            var user = new User { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                await _userManager.FindAsync(user.UserName, user.PasswordHash);
                // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }
    }
    catch(Exception ex)
    {

    }
    // If we got this far, something failed, redisplay form
    return View(model);
}

但正如我之前所说,从c#复制的生成guid用于在sql中插入表查询时抛出相同的错误。
经理:

public class ApplicationUserManager : UserManager<User, Guid>
{
    public ApplicationUserManager(IUserStore<User, Guid> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new CustomUserStore(context.Get<RecruitmentDbContext>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<User, Guid>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = true;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
        // You can write your own provider and plug it in here.
        //manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
        //{
        //    MessageFormat = "Your security code is {0}"
        //});
        //manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
        //{
        //    Subject = "Security Code",
        //    BodyFormat = "Your security code is {0}"
        //});
        //manager.EmailService = new EmailService();
        //manager.SmsService = new SmsService();
        //var dataProtectionProvider = options.DataProtectionProvider;
        //if (dataProtectionProvider != null)
        //{
        //    manager.UserTokenProvider = 
        //        new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        //}
        return manager;
    }
}
ee7vknir

ee7vknir1#

从Map器中删除以下行:

modelBuilder.Entity<User>()
            .Property(prop => prop.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

另外,从用户类中删除此项:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

因为您正在将其设置到代码中,而根据您的表创建sql查询,这不是数据库的责任。

相关问题