24

I have the following Model

public class FilanthropyEvent :  EntityBase, IDeleteable
{  
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime EventDate { get; set; }
    public string Description { get; set; }
    public decimal Target { get; set; }
    public decimal EntryFee { get; set; }
    public bool Deleted { get; set; }

    public ICollection<EventAttendee> EventAttendees { get; set; }
}

public class Attendee : EntityBase, IDeleteable
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool MailingList { get; set; }
    public bool Deleted { get; set; }

    public ICollection<EventAttendee> EventAttendees { get; set; }
}

Events and Attendees is a many to many relationship but I needed another property on the association so I created an association entity

 public class EventAttendee : EntityBase
 {
    public int FilanthropyEventId { get; set; }
    public int AttendeeId { get; set; }
    public bool InActive { get; set; }

    public virtual Attendee Attendee { get; set; }
    public virtual FilanthropyEvent FilanthropyEvent { get; set; }
 }

These are the configurations for each FilanthropyEvent and Attendee

public class FilanthropyEventConfiguration : EntityTypeConfiguration<FilanthropyEvent>
{
       public FilanthropyEventConfiguration()
       {
           HasKey(x => x.Id);
           Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

           HasMany(x => x.EventAttendees).WithRequired(x =>      x.FilanthropyEvent).HasForeignKey(x => x.FilanthropyEvent);
       }
}

public AttendeeConfiguration()
{
        HasKey(x => x.Id);
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        HasMany(x => x.EventAttendees).WithRequired(x => x.Attendee).HasForeignKey(x => x.AttendeeId);
}

public class EventAttendeesConfiguration : EntityTypeConfiguration<EventAttendee>
{
    public EventAttendeesConfiguration()
    {
        HasKey(x => new {x.FilanthropyEventId, x.AttendeeId});
    }
}

When I try and initialise the database via the update-database command in the package manager console I get the following error.

System.InvalidOperationException: The foreign key component 'FilanthropyEvent' is not a declared property on type 'EventAttendee'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.

I realise I'm probably missing a mapping in the EventAttendeesConfiguration class but what would be the correct mapping to model this relationship?

1 Answer 1

50

This code

HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEvent);

Should be

HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEventId);
2
  • 2
    Blimey(...) -- should be an exception type. I did the same thing ... Doh(...)
    – IAbstract
    Apr 8, 2016 at 19:17
  • :( guess this is a common stupid error, done the same as well, i blame copy and paste :) Jul 8, 2016 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.