Bradley Uffner
Bradley Uffner

Reputation: 16991

Entity Framework Navigation Woes

I have Business and BusinessProgram declared as:

 public class Business : DbIdEntity
    {
        public string Name { get; set; }
        public virtual Address PhysicalAddress { get; set; }
        public virtual Address PostalAddress { get; set; }

        public Guid OwnerKey { get; set; }
        public virtual Account Owner { get; set; }
        public virtual IEnumerable<BusinessProgram> BusinessPrograms { get; set;                   }

    }

public class BusinessProgram : DbEntity<Guid>
{
    public Business Business { get; set; }
    public ProgramType ProgramType { get; set; }

    public DateTime? EffectiveDate { get; set; }
    public DateTime? ExpireDate { get; set; }
}

DbIdEntity and DbEntity are just base classes where the primary key (and an autonumbering Id field are declared.

When I query it using this query

foreach (Data.Business business in context.Businesses.Include(b => b.Owner)
                                                     .Include(b => b.PhysicalAddress)
                                                     .Include(b => b.Owner)
                                                     .Include(b => b.BusinessPrograms)
                                                     .OrderBy(b => b.Name))

I'm also using a convention that makes properties ending in "Key" the primary and foreign keys instead of the default "Id".

I get the error:

"A specified Include path is not valid. The EntityType 'Data.Business' does not declare a navigation property with the name 'BusinessPrograms'."

What am I doing wrong?

UPDATE I used IEnumerable instead of ICollection. Using the correct navigation property type fixed the issue.

Upvotes: 0

Views: 14

Answers (2)

Bradley Uffner
Bradley Uffner

Reputation: 16991

I used IEnumerable for my navigation type instead of ICollection. Changing it to ICollection fixed the issue.

Upvotes: 1

Olly
Olly

Reputation: 6026

I've had rather limited success with using base classes in the way you describe. I'd try "flattening" your model first, and getting it working like that. Then you could try re-introducing the base classes; you might be able to get that working too.

Here it looks as though BusinessProgram should contain a FK property called BusinessProgram_BusinessId if you want to use the default convention. Alternatively, you could give it a different name and use an attribute to override the default convention:

[ForeignKey("Business")]
public int BusinessId { get; set;}

Upvotes: 0

Related Questions