2014-11-08 94 views
0

我是新的Asp.Net MVC。我在我的項目中使用了standart Membership提供程序。我有應用程序用戶和ApplicationDbContext無法保存實體框架中的嵌套對象

public class ApplicationUser : IdentityUser 
    { 
    [Required] 
     public virtual UserInfo UserInfo { get; set; } 
     public virtual ICollection<Lot> Lots { get; set; } 
    } 

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public DbSet<Lot> Lots { get; set; } 
     public DbSet<Product> Products { get; set; } 

     public DbSet<UserInfo> UserInfoes { get; set; } 

     public DbSet<PersonalInformation> PersonalInformations { get; set; } 
     public ApplicationDbContext() 
      : base("DefaultConnection") 
     { 
     } 
    } 

和的UserInfo

public class UserInfo 
    { 
     [Key] 
     public int UserInfoId { get; set; } 
     public DateTime RegistarationDate { get; set; } 
     public decimal Money { get; set; } 
     public decimal CurrentDebt { get; set; } 
     public virtual PersonalInformation PersonalInformation { get; set; } 
     public virtual ApplicationUser ApplicationUser { get; set; } 
    } 

個人信息

public class PersonalInformation 
    { 
     public int PersonalInformationId { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Telephone { get; set; } 
     public Adress Adress { get; set; } 
    } 

我RegisterViewModel

public class RegisterViewModel 
    { 
     [Required] 
     [Display(Name = "User name")] 
     public string UserName { get; set; } 

     [Required] 
     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
     [DataType(DataType.Password)] 
     [Display(Name = "Password")] 
     public string Password { get; set; } 

     [DataType(DataType.Password)] 
     [Display(Name = "Confirm password")] 
     [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
     public string ConfirmPassword { get; set; } 

     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public decimal Money { get; set; } 

     public string Telephone { get; set; } 
    } 

和行動創造控制器

[HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser() 
       { 
        UserName = model.UserName, 
        UserInfo = new UserInfo() 
        { 
         CurrentDebt = 0, 
         Money = model.Money, 
         RegistarationDate = DateTime.Now, 
         PersonalInformation = new PersonalInformation() 
         { 
          FirstName = model.FirstName, 
          LastName = model.LastName, 
          Telephone = model.Telephone, 
         } 
        } 
       }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInAsync(user, isPersistent: false); 
        return RedirectToAction("Index", "Home"); 
       } 
       else 
       { 
        AddErrors(result); 
       } 
      } 

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

當我填寫所有內容that're在行動中所提到的,我receivee這個錯誤

無法將NULL值插入列「UserInfoId」,表「DefaultConnection.dbo.UserInfoes」;列不允許有空值。 INSERT失敗。 該聲明已被終止。

有人能幫助我嗎?非常感謝

回答

0

您需要告訴EF,UserInfoIdKey,因此它將其映射爲Identity

像這樣的東西會在一個映射文件的工作:

public class UserInfoMap : EntityTypeConfiguration<UserInfo> 
{ 
    public UserInfoMap() 
    { 
     ToTable("UserInfo"); 
     HasKey(m => m.UserInfoId); 
    } 
} 

當代碼第一次遷移運行時,會產生這樣的:

CreateTable(
    "dbo.UserInfo", 
    c => new 
     { 
      UserInfoId = c.Int(nullable: false, identity: true) 
) 

這意味着你將不需要指定該ID將自動填充到數據庫中。