在表“AspNetUsers”上插入或更新违反外键约束

时间:2016-12-09 21:16:16

标签: c# .net asp.net-mvc asp.net-core registration

我正在尝试使用.net Core和MVC注册。我的AspNetUsers表链接到我的配置文件,如此(1到1):

enter image description here

这是代码中的关系:

// Relationship between Profiles and AspNetUsers: 0 to 0
            builder.Entity<ApplicationUser>()
                .HasOne(b => b.Profile)
                .WithOne(a => a.ApplicationUser)
                .HasForeignKey<ApplicationUser>(b => b.ProfileId);

但是当我尝试仅使用我的电子邮件和密码注册时,我收到此错误:

23503:在表“AspNetUsers”上插入或更新违反外键约束“FK_AspNetUsers_Profiles_ProfileId”

这是我的AccountController:

        public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {

                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {

                    await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    return RedirectToLocal(returnUrl);
                }
                AddErrors(result);
            }

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

这是我的ApplicationUser.cs:

namespace Overnight.Models.Security
{
     public class ApplicationUser : IdentityUser<Guid> 
     {
         public string PlainPassword  { get; set; }

         public DateTime CreatedAt {get; set;}

        public Nullable<DateTime> UpdatedAt {get; set;}

        public  Nullable<DateTime> DeletedAt {get; set;}

        public Int64 ProfileId { get; set; }

        public Profile Profile { get; set; }
    }
}

这是我的Profile.cs:

namespace Overnight.Models
{
      public enum GenderType : byte {
        Unknown = 0,
        Male = 1,
        Female = 2,
        NotApplicable = 9
    }
    public class Profile : BaseEntity<Int64>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; } 
        public GenderType Gender { get; set; }
        public Nullable<DateTime> DayOfBirth { get; set; } 
        public Nullable<DateTime> LastActivityDate { get; set; }
        //TODO One to One reference for image and adress 
        public List<ProfileReview> Reviews { get; set; }
        public List<Blog> Blogs { get; set; } 
         public List<Accomodation> Accomodations { get; set; } 
        public List<Post> Posts { get; set; }
         public List<Wishlist> Wishlists { get; set; }
        public List<Invoice> Invoices { get; set; }
        public List<Discount> Discounts { get; set; }

        public Security.ApplicationUser ApplicationUser { get; set; }

    }
}

我已经在每张桌子上播种了15行。

0 个答案:

没有答案