Invalid Column Name *Id

GPGVM

I know the problem: I am missing a mapping to help EF understand the relationship between my two tables but I am not seeing where my mistake is.

I have a Web Api using EF CF 7. I can query fine and get the parent table data but when I try to include the related child data I am getting the invalid column error.

My two tables look like this:

public class Categories
{
    private ICollection<SubCategories> _subcat;
    public int Id { get; set; }
    public string Category { get; set; }
    public virtual ICollection<SubCategories> SubCategories
    {
        get { return _subcat ?? (_subcat = new List<SubCategories>()); }
        set { _subcat = value; }
    }
}

public class SubCategories
{
    public int Id { get; set; }
    public string SubCategory { get; set; }
}

The above classes match my db schema EXCEPT the SubCategories table has a column CategoryId and it is a FK to the parent table Id column.

My repository query looks like this:

return _context.Categories.Include(i => i.SubCategories).ToList();

Now the error is throwing me...and I vaguely remember it has to do with EF concatenating the name. In that experience there was a base class that had the Id defined therefore when I defined it in my class it conflicted but that isn't the case here?

Invalid column name 'CategoriesId'.

So I have google and read several articles but either I didn't understand completely or they weren't my problem - probably the former.

AaronLS

The above classes match my db schema EXCEPT the SubCategories table has a column CategoryId and it is a FK to the parent table Id column.

If your table is named Categories, but your FK is named CategoryId then you'll have a problem. Inconsistent pluralization.

Either rename the FK to CategoriesId, or better yet follow the more common convention of singular class names(only instances of collections are plural) and change the table and class name to Category.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related