2

我正在使用 Visual Studio 中的 ASP.Net MVC 5。我想创建一个具有复杂类型的用户配置文件。我已经修改了文件中User类的代码IdentityModels.cs。这是代码:

public class User : IUser
{
    public User()
        : this(String.Empty)
    {
    }

    public User(string userName)
    {
        UserName = userName;
        Id = Guid.NewGuid().ToString();
    }

    [Key]
    public string Id { get; set; }

    public string UserName { get; set; }

    public string Phone { get; set; }
    public string MobilePhone { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
}

我还更改了此模型的视图。这很好用,没有任何问题。但是,如果我将Address上面的属性类型更改为Address,则意味着:public Address Address { get; set; }它失败了。

我已经尝试使用virtual关键字,但它没有用。请注意,每次我从头开始创建数据库表。另外,我检查了数据库,并使用正确的外键将信息插入到数据库中,但我不知道问题出在哪里。

代码执行失败,在下面的代码行await Users.Create(user)中返回false

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // Create a profile, password, and link the local login before signing in the user
                User user = new User(model.UserName)
                {
                    UserAddress = model.Address,
                    Email = model.Email,
                    Phone = model.Phone,
                    MobilePhone = model.MobilePhone
                };
                if (await Users.Create(user) &&
                    await Secrets.Create(new UserSecret(model.UserName, model.Password)) &&
                    await Logins.Add(new UserLogin(user.Id, IdentityConfig.LocalLoginProvider, model.UserName)))
                {
                    await SignIn(user.Id, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError(String.Empty, "Failed to create login for: " + model.UserName);
                }
            }
            catch (DbEntityValidationException e)
            {
                ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
            }
        }

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

更新:

如果您想知道,这是Address课程:

public class Address
{
    public int ID { get; set; }

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

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

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

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

更新 2

这是输入数据的图像: 在此处输入图像描述

4

2 回答 2

3

看来这是一个错误。在绝对证明之前,我不会选择这个作为答案。但是,当我从以下位置更改控制器的代码时:

                if (await Users.Create(user) &&
                    await Secrets.Create...

至:

                await Users.Create(user);
                if (await Secrets.Create...

它可以正常工作。在我看来,这应该是一个错误,因为我可以完美地加载编辑数据。

这意味着由于某种原因,即使IUserStore.Create成功,如果模型是复杂类型,它也会返回 false。

于 2013-08-04T09:30:33.293 回答
0

The problem that you have here (at least specific to changing Address to be an object rather than a string), is that your models aren't built correctly to relate them properly. Here is what you should be looking at.

public class User : IUser
{
    public User()
        : this(String.Empty)
    {
    }

    public User(string userName)
    {
        UserName = userName;
        Id = Guid.NewGuid().ToString();
    }

    [Key]
    public string Id { get; set; }    
    public string UserName { get; set; }
    public string Phone { get; set; }
    public string MobilePhone { get; set; }
    public string Email { get; set; }

    // This FK doesn't need to explicitly be declared, but I do so as it helps me
    // with the understanding of my structure a bit better.
    public int AddressId { get; set; }
    public Address Address { get; set; }
}

You also need to relate your Address back to your User class. I'm not sure how you want to do that, but, assuming that multiple people can live at the same address, you'll want a one-to-many relationship. (Right now, you receive an error because you don't specify the relationship.)

You have to do this in the Address model:

public class Address
{
    [Key]
    public int ID { get; set; }

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

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

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

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

    // I would give this a better property name than "Users" but just putting
    // this here for now.
    public virtual ICollection<User> Users { get; set; }
}

This way, when your database builds, Entity Framework can now properly build the relationships (where, before, it couldn't tell what you intended - hence the error when you switch over to Address).

Of course, there may be other issues, but, this is one that would cause problems.

于 2013-08-03T14:18:40.273 回答