0

So, I am working on a project dealing with a game. I have three projects right now, Web, Data, Data.EF. I would like the Web to not know anything about the concrete classes in Data.EF, but instead only reference the Interfaces in Data. The problem I am running into is that I have two classes Dungeon and AvailableDay. AvailableDay has two important members. Weekday which is a DayOfWeek and IEnumerable<IDungeon> Dungeons which is a "list" of dungeons that are available that day. The issue I am having is that I keep getting this error:

The navigation property 'Dungeons' is not a declared property on type 'AvailableDay'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

My AvailableDay mapping class is:

internal class AvailableDayMapping : EntityTypeConfiguration<AvailableDay>
{
    public AvailableDayMapping()
    {
        this.ToTable("AvailableDays", "dbo");
        this.HasKey(t => t.ID);
        this.HasKey(t => t.DungeonID);
        this.HasRequired(t => t.Dungeons).WithMany().HasForeignKey(t => t.DungeonID);
        this.Property(t => t.Weekday);
    }
}

AvailableDay class:

public class AvailableDay : IAvailableDay
{
    public int ID { get; set; }
    public int DungeonID { get; set; }
    public virtual IEnumerable<IDungeon> Dungeons { get; set; }
    public DayOfWeek Weekday { get; set; }
}

I am trying to query it by:

public async Task<IEnumerable<IAvailableDay>> GetAvailableDaysAsync()
{
    return await _Context.AvailableDays
                .Include(t => t.Dungeons)
                .GroupBy(t => t.Weekday).SelectMany(t => t).ToListAsync();
}

How can I pull in a list of Dungeon data while only allowing the Web to access it through interfaces?

ScubaSteve
  • 7,724
  • 8
  • 52
  • 65
  • try using the syntax _Context.AvailableDays.Include("Dungeons").GroupBy... Not saying your way is wrong I've just never seen that way of including. – Tim Southard Feb 07 '15 at 18:15
  • @Tim `using System.Data.Entity` lets you call [`Include()` with a property expression](https://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx). – CodeCaster Feb 07 '15 at 18:22
  • 1
    @CodeCaster thank you for pointing that out. Never done it that way before. – Tim Southard Feb 07 '15 at 18:23

1 Answers1

0

MSDN: Requirements for Creating POCO Proxies:

A navigation property that represents the "many" end of a relationship must return a type that implements ICollection, where T is the type of the object at the other end of the relationship. "

So change the property type:

public virtual Collection<Dungeon> Dungeons { get; set; }
CodeCaster
  • 147,647
  • 23
  • 218
  • 272