Stephen Panzer
Stephen Panzer

Reputation: 289

The foreign key component 'X' is not a declared property on type 'Y'. Verify that it has not been explicitly excluded

It seems this is a pretty common issue, but I've tried everything and can't see any issues.

It seems as if this issue just started today. Full error message:

"The foreign key component 'AllocationID' is not a declared property on type 'PortfolioSection'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property."

Model:

public class PortfolioSection 
{
    .... some stuff

    public int AllocationID { set; get; }

    [ForeignKey("AllocationID")]
    public virtual Allocation Allocation { get; set; }

    .... More stuff

}
public class Allocation 
{

    ... Some stuff



    [ForeignKey("PortfolioSections")]
    public int AllocationID { set; get; }


    public string Name { set; get;}


    public string Color { set; get; }


    public int SortOrder { set; get; }




    public virtual List<PortfolioSection> PortfolioSections { get; set; }


}

I'm not doing anything weird w/ the configuration that I can see that would cause this issue. Could it be that the key for Allocation is AllocationID and not just ID?

So far I've tried: 1. Removing the navigation property from PorfolioSection to Allocation. 2. Putting the ForiegnKey attribute on AllocationID (ForeignKey("Allocation")) instead of the navigation property.

Upvotes: 2

Views: 3817

Answers (1)

Martin Noreke
Martin Noreke

Reputation: 4146

You have a few attributes that are misplaced. If you change:

[ForeignKey("PortfolioSections")]
public int AllocationID { set; get; }

To:

[Key]
public int AllocationID { set; get; }

Then this should work fine. The issue is that you declared the Key (AllocationID is the primary key I am assuming) as a ForeignKey to the PortfolioSections table.

Upvotes: 1

Related Questions