'System.InvalidOperationException: Unable to determine the relationship represented by navigation property of Class.Property IEnumerable<AnotherClass>

This is new. Its saying it can't work out the property. Which is odd because it's quite well defined.

System.InvalidOperationException: Unable to determine the relationship represented by navigation 'TcaOrganisation.TcaUnits' of type 'IEnumerable'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

TcaOrganisation contains company definitions

TcaUnit contains vehicle definitions. There is a required field that contains the id of an organisation

Schema Diagram

The relationship in question is

ALTER TABLE [dbo].[TCA_Unit]  WITH CHECK 
    ADD CONSTRAINT [FK_TCA_Unit_TCA_Organisation] 
        FOREIGN KEY([OrgCode]) REFERENCES [dbo].[TCA_Organisation] ([OrgCode])
GO

ALTER TABLE [dbo].[TCA_Unit] CHECK CONSTRAINT [FK_TCA_Unit_TCA_Organisation]
GO

Inside the TcaUnit c# class, the references are:

    [ForeignKey("OrgCode")]
    [Description("A link to the parent Organisation of this Unit")]
    public virtual TcaOrganisation TcaOrganisation { get; set; }

    [ForeignKey("ServiceProviderOrgCode")]
    [Description("A link to the Service Provider this Unit")]
    public virtual TcaOrganisation TcaServiceProvider { get; set; }

    [ForeignKey("ServerId")]
    [Description("A link to the parent Server of this Unit")]
    public virtual TcaCtrackServer TcaCtrackServer { get; set; }

    [Description("A Collection of Scheme Enrolments for this Unit")]
    public virtual IEnumerable<TcaSchemeEnrolment> TcaSchemeEnrolments { get; set; }

While in TcaOrganisation we can see the relationship that is causing the issue:

    [ForeignKey("ServerId")]
    [Description("A link to the Server holding the Ctrack Unit details")]
    [DisplayName("Ctrack Server")]
    public virtual TcaCtrackServer TcaCtrackServer { get; set; }

    [Description("A collection of Units attached to the Organisation")]
    public virtual IEnumerable<TcaUnit> TcaUnits { get; set; }

    [Description("A collection of App Enrolments for the Organisation")]
    public virtual IEnumerable<TcaAppEnrolment> TcaAppEnrolments { get; set; }

    [Description("A collection of Scheme Enrolments for the Organisation")]
    public virtual IEnumerable<TcaSchemeEnrolment> TcaSchemeEnrolments { get; set; }

    [Description("The Service Provider for the Organisation")]
    [ForeignKey("ServiceProviderOrgCode")]
    public virtual TcaOrganisation ServiceProvider { get; set; }

This error is occurring on the first call to the context. (Not using these two tables btw) Why is my database schema suddenly invalid? How do I fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source