Asp.net MVC 4如何使用WebSecurity.createUserAndAccount与自定义字段 [英] Asp.net mvc 4 how to use WebSecurity.createUserAndAccount with custom field

查看:137
本文介绍了Asp.net MVC 4如何使用WebSecurity.createUserAndAccount与自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我创建用户配置 table.like

I have a problem with I create a custom field in UserProfile table.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;} 
     }

和我还添加了 UserDetail 的DbContext

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

问题是,当我使用

The Problem is when I use

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

它总是有一些错误出现这样的:从对象类型不存在任何映射...
但是如果我定义了一个简单类型(如字符串 INT )代替 UserDetail ,它工作正常。

It always comes up with some error like :No mapping exists from object type... But If I define a simple type (like string, int) instead of UserDetail, it works fine.

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

Anyone can help me solve this problem? Thanks very much!!

推荐答案

我有一个类似的问题,这得到它由工作:

I had a similar problem to this and got it work by:

沿着这条线结合UserDetail和用户配置的东西:

combine UserDetail and UserProfile to something along this line:

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]注册

update your [HttpPost] Register

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

不要忘了新的字段添加到您的RegisterModel需要

don't forget to add the new fields into your RegisterModel as needed

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

希望这对你的作品

hope this works for you

这篇关于Asp.net MVC 4如何使用WebSecurity.createUserAndAccount与自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆