1

I'm getting the following error when trying to establish the database relationship. But I couldn't establish a relationship:

Unable to determine the relationship represented by navigation 'Event.ApplicationUser' of type 'ApplicationUser'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'

  • A user can participate in more than one event.
  • A user can have multiple events.
  • An event can have multiple participants.
  • An event can be created by only one user.
public class Event : EntitySoftDeletableIntKey
{
    public string Title { get; set; }
    public string ShortContent { get; set; }
    public string Place { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int EventDetailId { get; set; } 
    public EventDetail EventDetail { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
    public List<Tag> Tags { get; set; } = new List<Tag>();
    public List<Comment> Comments { get; set; } = new List<Comment>();
    //This event was published by one user.
    public ApplicationUser ApplicationUser { get; set; }
    public int ApplicationUserId { get; set; }
    //Multiple participants attended this event.
    public List<ApplicationUser> Attendees { get; set; } = new List<ApplicationUser>();
    public List<FavoriteEvent> FavoriteEvents { get; set; }

}
public class ApplicationUser : IdentityUser<int>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<FavoriteEvent> FavoriteEvents { get; set; } = new List<FavoriteEvent>();
    //This user has posted multiple events.
    public List<Event> Events { get; set; } = new List<Event>();
    //this user has attended more then one events
    public List<Event> AttendedEvents { get; set; } = new List<Event>();

}

Thank you for answers...

dogac
  • 49
  • 10
  • I think this will help https://stackoverflow.com/questions/55629131/how-to-fix-unable-to-determine-the-relationship-represented-by-navigation-prope – ShubhamWagh Dec 27 '22 at 15:03
  • You are using Entity where there is a mapping between the c# classes/properties and the database tables/fields. The error is indicating the mapping needs updating. You need to do a migration which will update the database using the c# classes. Database first is no longer supported with Core. See : https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/ – jdweng Dec 27 '22 at 15:33
  • use attributes or in code mapping – T.S. Dec 27 '22 at 15:35
  • @jdweng `Database first is no longer supported with Core.` - could you please elaborate? Not looking for an extensive discussion in comments, but an absence of a visual designer is hardly enough to support this claim, given that scaffolding commands work just fine. – Sergey Kudriavtsev Dec 27 '22 at 16:06
  • Once the mapping is established all query commands to database will work. It is the updating of the mapping that is supported now in one direction only. Only class first is supported and database first not supported. With Core you use migration to update the mapping by making changes to the c# classes and then using migration to update the database. – jdweng Dec 27 '22 at 22:20
  • previously, EF core was creating an additional table for me in many to many relationships using the code first approach. but I think I need to use onmodelCreating when there are multiple relations. but when I wrote it , I started getting different errors. I just got my first job and I'm still learning. What method should I follow?Or can I get around this without using the onmodelconf method? – dogac Dec 28 '22 at 09:23

0 Answers0