可以';t在实体框架中保存嵌套对象
本文关键字:框架 保存 嵌套 对象 实体 可以 | 更新日期: 2023-09-27 18:29:44
我是Asp.Net MVC的新手。我在我的项目中使用standart会员服务提供商。我有应用程序用户和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);
}
当我填写所有在操作中提到的字段时,我收到了这个错误
无法将值NULL插入表"DefaultConnection.dbo.UserInfoes"的列"UserInfoId"中;列不允许为null。INSERT失败。语句已终止。
有人能帮我吗?非常感谢
您需要告诉EF UserInfoId
是表的Key
,以便它将其映射为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,因为它将自动填充到数据库中。