Ho un problema con creo un campo personalizzato in UserProfile
table.likeAsp.net MVC 4 come utilizzare WebSecurity.createUserAndAccount con il campo personalizzato
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public int? AddressId { get; set; }
public int? UserDetailId { get; set; }
public string UserName { get; set; }
public UserDetail UserDetail { get; set; }
}
public class RegisterModel
{
[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 virtual UserDetail UserDetail { get; set; }
}
public class UserDetail
{
public int Id{get;set;}
public string FirstName{get;set;}
public string LastName{get;set;}
}
E ho anche aggiunto UserDetail
-DbContext
classe
public DbSet<UserDetail> UserDetails{get;set;}
Il problema è quando uso
Web WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
new { UserDetail = new UserDetail()
}, false);
E alw ays si presenta con un errore come: Nessuna mappatura esiste dal tipo di oggetto ... Ma se definisco un tipo semplice (come string
, int
) anziché UserDetail
, funziona correttamente.
Chiunque può aiutarmi a risolvere questo problema? Grazie mille!!
che sto testare il tuo codice, ma dice mio contesto non contiene una definizione per ProfiloUtente o per UserDetails! Supponiamo di aggiungere qualcosa al nostro contesto db? Sono bloccato anche con questo! – Ciwan
@Ciwan vedi modifica nella risposta – Komengem
Grazie, sono riuscito a superare il problema che stavo avendo, tuttavia sto ottenendo 'null' su questa riga' var userProfile = context.UserProfiles.SingleOrDefault (u => u.UserName == model.UserName); 'nessun motivo per questo? Quando controllo Db, il nome utente è stato creato, ma la tabella UserDetails è vuota, ovviamente perché quella riga restituisce null. – Ciwan