2015-06-21 37 views
1

我已經爲ApplicationUser添加了一些屬性,其中兩個屬性是ICollection的屬性。 當我使用Update-Database時,它不會爲這兩個成員生成列。ApplicationUser ICollection成員未被保存在數據庫中

那麼,我在這裏錯過了什麼?我想這是非常基本的。我習慣於在Java中使用Hibernate,在那裏爲元素集合生成一個新表。

一些代碼樣本 -

ApplicationUser

public class ApplicationUser : IdentityUser 
{ 
    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    public byte[] UserImage { get; set; } 

    public virtual ICollection<string> Interests { get; set; } 

    public virtual ICollection<string> Friends { get; set; } 

} 

RegisterViewModel

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

    [Required] 
    [StringLength(25)] 
    [Display(Name = "First Name")] 
    public string FirstName { get; set; } 

    [Required] 
    [StringLength(25)] 
    [Display(Name = "Last Name")] 
    public string LastName { get; set; } 

    [Display(Name = "User Image")] 
    public int UserImage { get; set; } 

    [Required] 
    [Display(Name = "Interests")] 
    public virtual ICollection<string> Interests { get; set; } 

    [Display(Name = "Friends")] 
    public virtual ICollection<string> Friends { get; set; } 

    //........ 

任務寄存器(RegisterViewModel模型)

public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser() 
      { 
       UserName = model.UserName, 
       FirstName = model.FirstName, 
       LastName = model.LastName, 
       Interests = model.Interests, 
       Friends = model.Friends 
      }; 

      HttpPostedFileBase file = Request.Files["file"]; 
      byte[] imgBytes = null; 

      BinaryReader reader = new BinaryReader(file.InputStream); 
      imgBytes = reader.ReadBytes(file.ContentLength); 

      user.UserImage = imgBytes; 


      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      //............ 

回答

0

您需要爲收集項目建模。你可以多到很多或一對多。

// many to many 
public class Interest 
{ 
    public int InterestId { get; set; } 
    public string InterestDesc { get; set; } // field can't match class name 
} 

// one to many 
public class Interest 
{ 
    public int UserId { get; set; } // Make primary key the FK into application user 
    public string InterestDesc { get; set; } // field can't match class name 
} 

然後改變你的收藏

public virtual ICollection<Interest> Interests { get; set; } 

不要忘記將DbSet添加到背景。重複朋友和其他字符串集合。

+0

這樣做。謝謝! – omm118