2013-03-12 37 views
12

我有一個問題,我在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;} 
    } 

創建一個自定義字段,我還添加了UserDetailDbContext

public DbSet<UserDetail> UserDetails{get;set;} 

問題是,當我使用

Web WebSecurity.CreateUserAndAccount(model.UserName, model.Password, 
         new { UserDetail = new UserDetail() 
         }, false); 

它ALW ays會出現一些錯誤,如:沒有映射存在從對象類型... 但如果我定義一個簡單類型(如string,int)而不是UserDetail,它工作正常。

任何人都可以幫我解決這個問題嗎?非常感謝!!

回答

12

我也有類似的問題這一點,得到了它的工作:

沿着這條線結合UserDetail和用戶配置的東西:

public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 
    public string FirstName{get;set;}  
    public string LastName{get;set;} 
} 

更新您的[HttpPost]註冊

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, 
    propertyValues: new { FirstName= model.FirstName, LastName = model.LastName}, false); 

不要忘記根據需要將新字段添加到您的RegisterModel中

public class RegisterModel 
{ 
    .... 
    public string FirstName{get;set;}  
    public string LastName{get;set;} 
} 

希望這適用於你

4

我認爲你正在尋找這樣做。

UserDetail

public class UserDetail 
{ 
    //This is property mapping, UserId will be the same as the Membership UserId and UserProfile UserId 
    [Key] 
    [ForeignKey("UserProfile")] 
    [HiddenInput(DisplayValue = false)] 
    public int UserId { get; set; } 

    public string FirstName{get;set;}  
    public string LastName{get;set;} 

    public UserProfile UserProfile { get; set; } 
} 

RegisterModel

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; } 

    [Required] 
    public string FirstName{get;set;} 

    [Required] 
    public string LastName{get;set;} 
} 

註冊行動

// 
    // GET: /Account/Register 

    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/Register 

    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult Register(RegisterModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var db = new UsersContext(); 
      // Attempt to register the user 
      try 
      { 
       var userProfile = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, false); 

       //var userProfile= db.UserProfile.SingleOrDefault(u => u.UserName == model.UserName); 

       if (userProfile!= null) //This way Userdetail is only created if UserProfile exists so that it can retrieve the foreign key 
       { 
        var userDetail = new UserDetail 
              { 
               UserProfile = userProfile, 
               FirstName = model.FirstName, 
               LastName = model.LastName 
              };             

        db.UserDetails.Add(userDetail); 
        db.SaveChanges(); 
       }      
      } 
      catch (MembershipCreateUserException e) 
      { 
       ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

編輯

根據您如何設置您的語境和資料庫,你會需要像下面的每個POCO類

public DbSet<UserProfile> UserProfiles { get; set; } 
    IQueryable<UserProfile> IDataSource.UserProfiles 
    { 
     get { return UserProfiles; } 
    } 

除了SimpleMembership表,你將需要添加下面對上下文的many-to-manyMembership and Roles

+0

之間

protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Membership>() .HasMany<Role>(r => r.Roles) .WithMany(u => u.Members) .Map(m => { m.ToTable("webpages_UsersInRoles"); m.MapLeftKey("UserId"); m.MapRightKey("RoleId"); }); } 
我測試你的代碼,但它說我的情況下不包含定義用戶配置或者是UserDetails!我們是否想將某些東西添加到我們的數據庫環境中?我也堅持這一點! – Ciwan 2013-05-28 22:26:48

+0

@Ciwan請參閱編輯答案 – Komengem 2013-05-28 22:35:56

+0

謝謝,我設法克服了我遇到的問題,但是我在這行上得到'null' var userProfile = context。UserProfiles.SingleOrDefault(u => u.UserName == model.UserName);'這是什麼原因?當我檢查Db時,用戶名已創建,但UserDetails表爲空,顯然是因爲該行返回空值。 – Ciwan 2013-05-28 22:57:54

相關問題