12

我在UserProfiletable.like中创建自定义字段时遇到问题

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

它总是会出现一些错误,例如:对象类型不存在映射...但是如果我定义一个简单的类型(例如string, int)而不是UserDetail,它可以正常工作。

任何人都可以帮我解决这个问题吗?非常感谢!!

4

2 回答 2

12

我遇到了与此类似的问题,并通过以下方式使其工作:

将 UserDetail 和 UserProfile 结合到这一行:

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

希望这对你有用

于 2013-03-13T22:32:13.413 回答
4

我认为您正在寻求这样做。

用户详情

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

注册模型

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

    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");
          });
    }
于 2013-03-13T23:11:55.607 回答