1

错误:

No mapping exists from object type eTrail.Models.Global.Address to a known managed provider native type.

引发错误的代码:

if (!WebSecurity.UserExists("me"))
        {
            WebSecurity.CreateUserAndAccount(
                "me",
                "password", new
                {
                    FirstName = "Firstname",
                    LastName = "Lastname",
                    Email = "me@me.com",
                    Address = new Address
                    {
                        Street = "123 Stree",
                        Street2 = "",
                        City = "CityVille",
                        State = "UT",
                        Zip = "99999",
                        Country = "USA",
                        PhoneCell = "111.111.1111"
                    },
                    CreatedDate = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    ImageName = ""
                });
        }

我的 User.cs 模型:

    public class User : IAuditInfo
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; } 
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    public Address UserAddress { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
    public ICollection<Role> Roles { get; set; }
    public string ImageName { get; set; }

    public User()
    {
        UserAddress = new Address();
        Roles = new List<Role>();
    }
}

地址模型:

public class Address
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Street { get; set; }
    public string Street2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
    public string PhoneHome { get; set; } 
    public string PhoneCell { get; set; } 
    public string PhoneOther { get; set; }
    public string FaxNumber { get; set; }
}

知道为什么我会收到此错误吗?这两个模型类在我的 DbContext 类中作为 DbSet 和 DbSet。

4

1 回答 1

1

创建新用户帐户时使用了错误的属性名称。您使用了Address而不是UserAddress。在我添加到代码中的注释下进行以下更改。

if (!WebSecurity.UserExists("me"))
    {
        WebSecurity.CreateUserAndAccount(
            "me",
            "password", new
            {
                FirstName = "Firstname",
                LastName = "Lastname",
                Email = "me@me.com",
                //Changed Address to UserAddress
                UserAddress = new Address
                {
                    Street = "123 Stree",
                    Street2 = "",
                    City = "CityVille",
                    State = "UT",
                    Zip = "99999",
                    Country = "USA",
                    PhoneCell = "111.111.1111"
                },
                CreatedDate = DateTime.Now,
                ModifiedDate = DateTime.Now,
                ImageName = ""
            });
    }
于 2013-09-12T13:04:11.650 回答