I have the following model:
public class Hotfix
{
public int? released_version { get; set; }
public virtual ReleaseVersion ReleasedVersion { get; set; }
}
In my mapping class (which is correctly being looked at) I have:
this.Property(t => t.released_version).HasColumnName("released_version");
this.HasOptional(t => t.ReleasedVersion)
.WithMany(t => t.ReleaseVersionForHotfix)
.HasForeignKey(d => d.released_version);
However, when I perform a query such as _context.Hotfixes.ToList() I get the exception Invalid column name 'ReleaseVersion_id'..
Why is it looking for a ReleaseVersion_id column when it’s being told the column should be mapped to released_version?
This is using CodeFirst on an existing database
Ok I finally figured this out, and apparently it was due to some code not shown. There seems to be a bug in EF’s fluent mapping when extra properties exist that are not mapped. I should have shown the
ReleaseVersionstructure which looks like:What’s significant is the extra
hotfixes2property. This property was created by the EF 4.1 CodeFirst power tools, but I didn’t remove it because I wanted to get everything working first.Anyways, the
hotfixes2property had no relationship mapping in the hotfix EF configuration mapping class but for some reason this one extra property was causing the whole hotfix configuration to be disgarded. Simply removing thehotfixes2property from theReleaseVersionPOCO caused my application to correctly work without errors.