Entity framework ForeignKeyAttribute on property … on type … is not valid

c# entity-framework entity-framework-6

Question

I get this error:

The ForeignKeyAttribute on property 'SubdivisionsHOA' on type 'HealthOfAustralia.DAL.Models.ContentArticleHOAsubdivision' is not valid. The foreign key name 'SubdivisionHOAId' was not found on the dependent type 'HealthOfAustralia.DAL.Models.SubdivisionHOA'. The Name value should be a comma separated list of foreign key property names.

Here are the model classes:

public partial class ContentArticleHOAsubdivision
{
    public int Id { get; set; }
    public long ContentArticleId { get; set; }
    public short SubdivisionHOAId { get; set; }

    [ForeignKey("ContentArticleId")]
    public virtual ContentArticleHOA ContentArticleHOA { get; set; }
    [ForeignKey("SubdivisionHOAId")]
    public virtual ICollection<SubdivisionHOA> SubdivisionsHOA { get; set; }
}

public partial class SubdivisionHOA
{
    [Key, ForeignKey("TopTierDivisionHOA")]
    public short Id { get; set; }
    public string Name { get; set; }

    public virtual TopTierDivisionHOA TopTierDivisionHOA { get; set; }
}

What am I doing wrong?

1
2
7/10/2014 1:41:39 PM

Popular Answer

You need to choose one to many relation and use the following code:

public class ContentArticleHOAsubdivision
{
     ...Comment this
     //public short SubdivisionHOAId { get; set; }
     ...
     [InverseProperty(" create property for ContentArticleHOAsubdivision on SubdivisionHOA")]
     public virtual ICollection<SubdivisionHOA> SubdivisionsHOA { get; set; }
}

or one to one with this one:

[ForeignKey("SubdivisionHOA")]
public short SubdivisionHOAId { get; set; }

public virtual SubdivisionHOA SubdivisionsHOA { get; set; }
2
8/3/2016 10:01:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow