我有一個問題,我在UserProfile
table.likeAsp.net MVC 4如何使用WebSecurity.createUserAndAccount與自定義字段
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;}
}
創建一個自定義字段,我還添加了UserDetail
到DbContext
類
public DbSet<UserDetail> UserDetails{get;set;}
問題是,當我使用
Web WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
new { UserDetail = new UserDetail()
}, false);
它ALW ays會出現一些錯誤,如:沒有映射存在從對象類型... 但如果我定義一個簡單類型(如string
,int
)而不是UserDetail
,它工作正常。
任何人都可以幫我解決這個問題嗎?非常感謝!!
之間
我測試你的代碼,但它說我的情況下不包含定義用戶配置或者是UserDetails!我們是否想將某些東西添加到我們的數據庫環境中?我也堅持這一點! – Ciwan 2013-05-28 22:26:48@Ciwan請參閱編輯答案 – Komengem 2013-05-28 22:35:56
謝謝,我設法克服了我遇到的問題,但是我在這行上得到'null' var userProfile = context。UserProfiles.SingleOrDefault(u => u.UserName == model.UserName);'這是什麼原因?當我檢查Db時,用戶名已創建,但UserDetails表爲空,顯然是因爲該行返回空值。 – Ciwan 2013-05-28 22:57:54