C
C#16mo ago
Pedro

❔ EntityFramework help determining relationship

System.InvalidOperationException: Unable to determine the relationship represented by navigation 'Event.Planner' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'. User.cs
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class User : IdentityUser
{
public List<Event> Events { get; set; } = new List<Event>();
}
}
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class User : IdentityUser
{
public List<Event> Events { get; set; } = new List<Event>();
}
}
Event.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class Event
{
public int Id { get; set; }

[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }

[Required(ErrorMessage = "Planner is required.")]
public User Planner { get; set; }

[Required(ErrorMessage = "At least one participant is required.")]
public List<User> Users { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Identity;

namespace EventEnroll.Models
{
public class Event
{
public int Id { get; set; }

[Required]
[MinLength(8, ErrorMessage = "Title must be at least 8 characters.")]
public string Title { get; set; } = string.Empty;

[MaxLength(50, ErrorMessage = "Description cannot exceed 50 characters.")]
public string Description { get; set; } = string.Empty;

[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }

[Required(ErrorMessage = "Planner is required.")]
public User Planner { get; set; }

[Required(ErrorMessage = "At least one participant is required.")]
public List<User> Users { get; set; }
}
}
11 Replies
mg
mg16mo ago
You need to properly handle the many-to-many relationship between users and events. Also, don't mix your models. It looks like you have some data annotations for form validation in there, but this should only be for interacting with the database.
Pedro
PedroOP16mo ago
should i do this by creating a EventUser class?
mg
mg16mo ago
Yep
Pedro
PedroOP16mo ago
what would be the difference by creating the new class and using something like this
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasMany(e => e.Users)
.WithMany(e => e.Events);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasMany(e => e.Users)
.WithMany(e => e.Events);
}
Jimmacle
Jimmacle16mo ago
you don't need an explicit join entity iirc
Pedro
PedroOP16mo ago
btw is this a good design? or should i only store the users on the event class
Jimmacle
Jimmacle16mo ago
depends how you need to access them
Pedro
PedroOP16mo ago
my ideia initially was to make a user with a list of events that he created but now im wondering whats easier/better to implement i could just store the users inside the event class and when i try to get all the events that an user participates/has created, i just iterate over their ids
Jimmacle
Jimmacle16mo ago
keep in mind you aren't "storing" these in either, no matter what you do you will always have a join table between users and events in your database the navigation properties just expose that relationship to you whether you want it from both sides or just one is up to you and how you want to control how you can interact with the model
mg
mg16mo ago
oh TIL
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server