我已经向ApplicationUser添加了一些属性,其中两个是ICollection。 当我使用Update-Database时,它不会为这两个成员生成列。
那么,我在这里错过了什么?我认为这是非常基本的。我以前在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 :(得分: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添加到上下文中。对Friends和其他字符串集重复。