C# 数据库中未保存ApplicationUser ICollection成员
我已经向ApplicationUser添加了一些属性,其中两个是ICollection的。 当我使用更新数据库时,它不会为这两个成员生成列 那么,我错过了什么?我想这是非常基本的。我习惯于在Java中使用Hibernate,它为元素集合生成一个新表 一些代码示例- 应用程序用户C# 数据库中未保存ApplicationUser ICollection成员,c#,.net,entity-framework,asp.net-mvc-4,asp.net-mvc-5,C#,.net,Entity Framework,Asp.net Mvc 4,Asp.net Mvc 5,我已经向ApplicationUser添加了一些属性,其中两个是ICollection的。 当我使用更新数据库时,它不会为这两个成员生成列 那么,我错过了什么?我想这是非常基本的。我习惯于在Java中使用Hibernate,它为元素集合生成一个新表 一些代码示例- 应用程序用户 public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string Las
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; }
}
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字节[]用户映像{get;set;}
公共虚拟ICollection兴趣{get;set;}
公共虚拟ICollection好友{get;set;}
}
注册服务模型
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; }
//........
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)
//............
公共类RegisterViewModel
{
[必需]
[显示(Name=“Username”)]
公共字符串用户名{get;set;}
[必需]
[第25段]
[显示(Name=“First Name”)]
公共字符串名{get;set;}
[必需]
[第25段]
[显示(Name=“Last Name”)]
公共字符串LastName{get;set;}
[显示(Name=“用户图像”)]
public int UserImage{get;set;}
[必需]
[显示(Name=“兴趣”)]
公共虚拟ICollection兴趣{get;set;}
[显示(Name=“Friends”)]
公共虚拟ICollection好友{get;set;}
//........
任务寄存器(RegisterViewModel模型)
公共异步任务寄存器(RegisterViewModel模型)
{
if(ModelState.IsValid)
{
var user=new ApplicationUser()
{
用户名=model.UserName,
FirstName=model.FirstName,
LastName=model.LastName,
利益=模型。利益,
朋友=模特。朋友
};
HttpPostedFileBase file=Request.Files[“file”];
字节[]imgBytes=null;
BinaryReader=新的BinaryReader(file.InputStream);
imgBytes=reader.ReadBytes(file.ContentLength);
user.UserImage=imgBytes;
var result=await UserManager.CreateAsync(用户、模型、密码);
if(result.successed)
//............
您需要对收藏项目进行建模。您可以选择多对多或一对多
// 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; }
公共虚拟ICollection兴趣{get;set;}
不要忘记将数据库集添加到上下文中。对好友和其他字符串集合重复此操作。您需要对集合项进行建模。您可以选择多对多或一对多
// 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; }
公共虚拟ICollection兴趣{get;set;}
不要忘记将数据库集添加到上下文中。对好友和其他字符串集合重复此操作