【问题标题】:EF5 Migrations throwing error on simplemembership seedEF5 迁移在 simplemembership 种子上引发错误
【发布时间】:2013-09-12 21:04:11
【问题描述】:

错误:

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。

【问题讨论】:

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


    【解决方案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 = ""
                });
        }
    

    【讨论】:

    • 你是我的英雄指出这样一个菜鸟错误。像魅力一样工作。有证据表明智能感知既是一种出色的工具,又是开发人员有时过度依赖的拐杖
    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 2023-03-12
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2013-04-04
    • 2014-08-14
    • 1970-01-01
    相关资源
    最近更新 更多