Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

c# - Entity Framework 5 Invalid Column Name error

I'm currently writing a billing application using EF 5 Code First, and I'm running into an error when I'm running the application.

The database object in question is as follows:

[Table("Client")]
public class ClientBase
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ClientID { get; set; }

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

    [Required]
    public bool IsActive { get; set; }

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

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

    [Required]
    public DateTime ClientStartDate { get; set; }

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

    public DateTime TerminatedDate { get; set; }

    public string ClientStreet { get; set; }

    public string ClientCity { get; set; }

    public string ClientState { get; set; }

    public int? ClientZipCode { get; set; }

    public virtual List<PropertyBase> Properties { get; set; }

    public virtual List<ClientCharge> ClientDefaultCharges { get; set; }

}

I recently added a bunch of those fields (From ClientStartDate down to ClientZipCode are all new), and whenever I run the application I get the following error:

{"Invalid column name 'ClientStartDate'.
Invalid column name 'SalesforceID'.
Invalid column name 'TerminatedDate'.
Invalid column name 'ClientStreet'.
Invalid column name 'ClientCity'.
Invalid column name 'ClientState'.
Invalid column name 'ClientZipCode'."}

What amazes me, though, is that my database actually has updated accordingly. Those fields are now on the table, but this is still giving me an error.

Any ideas for what's going wrong here?

EDIT: Ok, there apparently was one thing I forgot to mention: SalesforceID is NOT a foreign key. None of the columns that were added were actually FKs. They are just plain fields.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In short, consider making your [Required] DateTime fields Nullable (DateTime?). If you provide more information about the stacktrace and any Initialization code it would be helpful.

[Required]
public DateTime? ClientStartDate { get; set; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...