将Guid.NewGuid添加到SQL数据库问题中

时间:2018-01-06 11:37:14

标签: c# mysql asp.net-mvc entity-framework

我在将新记录添加到数据库中的Users表时遇到问题。我有ID作为uniqueidentifier,我尝试使用Guid.NewGuid()作为此ID从C#添加新记录。

SQL返回错误,该ID不能为null。我正在尝试使用这个生成的Guid ID在数据库中添加记录,我看到同样的错误,但如果我将使用sql NewId添加记录,我会看到该记录已成功添加。

我使用EF的Code First创建了数据库。 Project在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#Controller中的注册操作错误正在抛出

[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);
}

但正如我之前所说,在SQL中从C#复制到插入表查询的生成GUID会引发相同的错误。

管理器:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

从映射器中删除以下行:

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

另外,从User类中删除它:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

因为您将其设置为代码,并且根据您的表创建SQL查询,它不是数据库的责任。