codeandcloud
codeandcloud

Reputation: 55200

Invalid column name TableName_ID error

I have two tables

  1. PropertyListing - It stores the details of the property user add, with an FK
  2. PropertyAvailability - It's a table that stores property status ( Now Available, After 3 Months, ...)

I am trying to enforce a one-to-many relation with these two tables (Fluent API) like this

public partial class PropertyListing
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string StreetAddress { get; set; }
    //the column that links with PropertyAvaibility table PK
    public byte? Availability { get; set; }

    public bool Status { get; set; }

    public virtual PropertyAvailability PropertyAvailability { get; set; }
}

public partial class PropertyAvailability
{
    public byte ID { get; set; }
    public string Status { get; set; }

    public virtual ICollection<PropertyListing> PropertyListings { get; set; }
    public PropertyAvailability()
    {
        PropertyListings = new List<PropertyListing>();
    }
}

I am calling this on OnModelCreating

modelBuilder.Entity<PropertyListing>()
        .HasRequired(pl => pl.PropertyAvailability)
        .WithMany(pa => pa.PropertyListings)
        .HasForeignKey(pl => pl.Availability);

It fails with this error,

Invalid column name 'PropertyListing_ID'.

Tutorial I used: http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

What could be wrong? I know I have screwed up the naming convention EF6 expects, but isn't there a workaround?

P.S: I have seen this question asked from ef3 or so in our SO, but I am unable to find any solution and hence the question.

Upvotes: 1

Views: 405

Answers (1)

jvanrhyn
jvanrhyn

Reputation: 2824

Add the Column attribute to you class

public partial class PropertyListing
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("ID")]
    public int ID { get; set; }
    public string StreetAddress { get; set; }
    //the column that links with PropertyAvaibility table PK
    public byte? Availability { get; set; }

    public bool Status { get; set; }

    public virtual PropertyAvailability PropertyAvailability { get; set; }
}

Upvotes: 1

Related Questions