Я новичок в Asp.Net MVC. Я использую стандартного поставщика членства в своем проекте. У меня есть пользовательских приложений и 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);
}
И когда я заполняю все поля that're упоминается в действии, я receivee эту ошибку
Не удается вставить значение NULL в столбце «UserInfoId», таблица «DefaultConnection.dbo.UserInfoes»; столбец не допускает нулей. INSERT терпит неудачу. Заявление было прекращено.
Может ли кто-нибудь мне помочь? Большое спасибо