使用复杂类型的列名无效

时间:2014-05-28 16:12:37

标签: entity-framework ef-code-first

我收到以下错误:

  

SqlException(0x80131904):

     

无效的列名'FirstName'。

     

无效的列名称“LastName”。

     

无效的列名'FirstName'。

     

无效的列名称“LastName”。

     

无效的列名'FirstName'。

     

无效的列名称“LastName”。

首先,我对为什么每列都有三个错误感到困惑。 名字和姓氏在复杂类型中的代码:

    [ComplexType]
    public class FullName : ValueObject<FullName> {

    public FullName(string firstName, string lastName) {
        FirstName = firstName;
        LastName = lastName;
    }

    public FullName(FullName fullName)
        : this(fullName.FirstName, fullName.LastName) {
    }

    internal FullName() {
    }

    [Required]
    [MaxLength(25, ErrorMessage="A maximum of 25 characters is allowed.")]
    [Column("FirstName")]
    public string FirstName { get; private set; }

    [Required]
    [MaxLength(25, ErrorMessage = "A maximum of 25 characters is allowed.")]
    [Column("LastName")]
    public string LastName { get; private set; }

    public string AsFormattedName() {
        return this.FirstName + " " + this.LastName;
    }

    public FullName WithChangedFirstName(string firstName) {
        return new FullName(firstName, this.LastName);
    }

    public FullName WithChangedLastName(string lastName) {
        return new FullName(this.FirstName, lastName);
    }
    public override string ToString() {
        return "FullName [firstName=" + FirstName + ", lastName=" + LastName + "]";
    }
}

ValueObject类来自Jimmy Bogard博客: http://grabbagoft.blogspot.co.uk/2007/06/generic-value-object-equality.html

这是在ApplicationUser类中使用的,它从Microsoft.AspNet.Identity.EntityFramework扩展IdentityUser。

public partial class ApplicationUser : IdentityUser {

    [Required]
    public FullName Fullname { get; private set; }
}

谢谢!

埃里克。

2 个答案:

答案 0 :(得分:0)

如果要检索数据,则FirstName和LastName属性的setter不能是私有的。尝试将属性更改为:

public string FirstName { get; set; }
public string LastName { get; set; }

答案 1 :(得分:0)

好的,我明白了。我不是完全确定解决方案是什么,但我有一个数据库现有的数据库具有相同的名称,但不同的情况。我删除了这个,并从

更改了Global.asax中的行

Database.SetInitializer(new DropCreateDatabaseIfModelChanges());

Database.SetInitializer(new DropCreateDatabaseAlways());