2015-06-21 36 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