EF5迁移在simplemembership种子上抛出错误

时间:2013-09-10 10:59:12

标签: asp.net-mvc asp.net-mvc-4 entity-framework-4 entity-framework-5 simplemembership

错误:

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

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

1 个答案:

答案 0 :(得分:1)

您在创建新用户帐户时使用了错误的属性名称。您使用地址而不是 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 = ""
            });
    }