1

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.

1 Answer 1

1

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; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I thought the column attribute was used if the property name was different? How does this solve the issue? Just curious.
very interesting. why did you think so? it didnt work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.