简体   繁体   English

ASP.NET不存在从对象类型XXX.Models.AccountRole到已知的托管提供程序本机类型(一对多关系)的映射

[英]ASP.NET No mapping exists from object type XXX.Models.AccountRole to a known managed provider native type, one to many relation

I try to register an user and he have a default attributes " Role ", set at " USER ". 我尝试注册一个用户,他有一个默认属性“ Role ”,设置为“ USER ”。 This attribute provides from a related table ' AccountRole ' but I've this error : 此属性从相关表“ AccountRole ”提供,但出现此错误:

No mapping exists from object type XXX.Models.AccountRole to a known managed provider native type. 从对象类型XXX.Models.AccountRole到已知的托管提供程序本机类型没有映射。

I tried this : 我尝试了这个:

DbEntities.cs DbEntities.cs

public class DbEntities : DbContext
{
    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<AccountRole> AccountRoles { get; set; }
}

AccountRole.cs AccountRole.cs

[Table("AccountRole")]
public class AccountRole
{
    public AccountRole()
    {
        Users = new List<UserProfile>();
    }
    //[HiddenInput(DisplayValue = false)]
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int AccountRoleId { get; set; }
    [Required]
    public string AccountRoleLabel { get; set; }

    public virtual ICollection<UserProfile> Users { get; set; }
}

UserProfile 用户资料

[Table("UserProfile")]
public class UserProfile
{
    public UserProfile() {

    }

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Name { get; set; }

    public virtual AccountRole Role { get; set; } 

}

RegisterModel RegisterModel

public class RegisterModel
{

EDIT 编辑

    public RegisterModel()
    {
        DbEntities db = new DbEntities();
        AccountRole accountrole = db.AccountRoles.Find(1);
        AccountRole Role = new AccountRole();
        this.Role = accountrole;
    }

FIN EDIT 结束编辑

    [Required]
    [Display(Name = "Nom d'utilisateur")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "La chaîne {0} doit comporter au moins {2} caractères.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Mot de passe")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirmer le mot de passe ")]
    [Compare("Password", ErrorMessage = "Le mot de passe et le mot de passe de confirmation ne correspondent pas.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Nom complet ")]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Text)]
    public AccountRole Role { get; set; }
}

AccountController 的AccountController

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Tentative d'inscription de l'utilisateur
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Name = model.Name, Role = model.Role }, false);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // Si nous sommes arrivés là, quelque chose a échoué, réafficher le formulaire
        return View(model);
    }

Error : No mapping exists from object type XXX.Models.AccountRole to a known managed provider native type. 错误 :从对象类型XXX.Models.AccountRole到已知的托管提供程序本机类型的映射不存在。

EDIT : After reflexion, get Role in role table it's better than recreate new one. 编辑 :反思后,获得角色表中的角色比重新创建新角色要好。 But I always the same error : 但是我总是一样的错误:

No mapping exists from object type System.Data.Entity.DynamicProxies.AccountRole_9E84587E3FD1A8E8356A2B8C874378A4725E1429BA029DF56367134D7C318AF4 to a known managed provider native type. 从对象类型System.Data.Entity.DynamicProxies.AccountRole_9E84587E3FD1A8E8356A2B8C874378A4725E1429BA029DF56367134D7C318AF4到已知的托管提供程序本机类型之间没有映射。

It's happened when I try to register someone 当我尝试注册某人时发生了

Thank you ;) 谢谢 ;)

It was so easy... 太简单了...

After lots of debugging I understand my mistake.. 经过大量的调试,我了解我的错误..

AccountController 的AccountController

    //
    // POST: /Account/Register

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {

        DbEntities db = new DbEntities();
        AccountRole accountrole = db.AccountRoles.First(x => x.AccountRoleLabel=="USER");
        model.Role = accountrole;

        if (ModelState.IsValid)
        {
            // Tentative d'inscription de l'utilisateur
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Name = model.Name, AccountRoleRefId = model.Role.AccountRoleId}, false);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // Si nous sommes arrivés là, quelque chose a échoué, réafficher le formulaire
        return View(model);
    }

CORRECTION 更正

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Name = model.Name, AccountRoleRefId = model.Role.AccountRoleId}, false);

AND NOT THIS: 而不是这样:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Name = model.Name, Role = model.Role}, false);

Normal, Role isn't the name of column, it's AccountRoleRefId And, model.Role is an AccountRole not a int 正常情况下, Role不是列的名称,而是AccountRoleRefId并且model.RoleAccountRole而不是int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从对象类型System.Net.Sockets.SocketException到已知托管提供程序本机类型的映射不存在 - No mapping exists from object type System.Net.Sockets.SocketException to a known managed provider native type ado.net“不存在从对象类型xo.Player到已知托管提供程序本机类型的映射。” - ado.net “No mapping exists from object type xo.Player to a known managed provider native type.” 从ObjectParameter到已知的托管提供程序本机类型不存在映射 - No mapping exists from ObjectParameter to a known managed provider native type 不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射 - No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type 从对象类型System.Data.Spatial.DbGeography到已知的托管提供程序本机类型不存在映射 - No mapping exists from object type System.Data.Spatial.DbGeography to a known managed provider native type 从对象类型System.Windows.Forms.DateTimePicker到已知的托管提供程序本机类型不存在映射 - No mapping exists from object type System.Windows.Forms.DateTimePicker to a known managed provider native type 从对象类型System.Web.UI.WebControls.ListItem到已知托管提供程序本机类型的映射不存在 - No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type 从对象类型System.Int32 []到已知的托管提供程序本机类型不存在映射 - No mapping exists from object type System.Int32[] to a known managed provider native type 不存在从对象类型 System.Windows.Forms.RichTextBox 到已知托管提供程序本机类型的映射。 - No mapping exists from object type System.Windows.Forms.RichTextBox to a known managed provider native type.' 从对象类型System.Web.UI.WebControls.GridViewRow到已知托管提供程序本机类型的映射不存在 - No mapping exists from object type System.Web.UI.WebControls.GridViewRow to a known managed provider native type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM