6

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'.\r\nInvalid column name 'SalesforceID'.\r\nInvalid column name 'TerminatedDate'.\r\nInvalid column name 'ClientStreet'.\r\nInvalid column name 'ClientCity'.\r\nInvalid column name 'ClientState'.\r\nInvalid 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.

Corey Adler
  • 15,897
  • 18
  • 66
  • 80

3 Answers3

9

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; }
kingdango
  • 3,979
  • 2
  • 26
  • 43
7

In my case this error happened because I was updating my EDMX against my testing database and running my code against the production database.

CLRBTH
  • 512
  • 4
  • 9
1

My suspicion is that SalesforceID is causing the problem. Try removing all of the new columns and adding them back one at a time, checking for errors as you go. If the problem is indeed with SalesforceID, this may solve it.

Community
  • 1
  • 1
Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
  • It's not a FK, though. And I have tried it with one at a time, and it still all fails. – Corey Adler May 09 '12 at 15:45
  • Hmm... Here are some other things to look at. Is the problem limited to this class only? Classes with data annotations modifying the table name? Can you create an identical class but change the name and see if you still get the error? Use a non-beta version of EF? – Jon Crowell May 09 '12 at 16:10
  • I have other classes with the same annotations that work. And I switched back to EF 4.3. Still nothing. – Corey Adler May 09 '12 at 19:19
  • Not sure what else you should try. Hopefully somebody else will answer. – Jon Crowell May 09 '12 at 19:43