C
C#11mo 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
mg11mo 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
Pedro11mo ago
should i do this by creating a EventUser class?
mg
mg11mo ago
Yep
Pedro
Pedro11mo 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
Jimmacle11mo ago
you don't need an explicit join entity iirc
Pedro
Pedro11mo ago
btw is this a good design? or should i only store the users on the event class
Jimmacle
Jimmacle11mo ago
depends how you need to access them
Pedro
Pedro11mo 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
Jimmacle11mo 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
mg11mo ago
oh TIL
Accord
Accord11mo 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
More Posts
❔ HttpContext reading Request and Response bodyI use serilog for my logging system. My aim is that converting Request body and response body to st❔ Need help with publishingWe use click-once deployment. I am using Visual Studio Publish Wizard. I incremented the version num❔ IOptions pattern in WebAPI getting null valueI'm trying to use the IOptions pattern with Dep. Injection, however when i debug the code i get null❔ Button click is advancing control focusI am writing a program which runs a test after the user clicks a button. The test takes a couple sec❔ If you needed to bulk sync data from an API into a DB, what would you use as a PK for child data?For example imagine you get this data back HOURLY from GET /users: ```json { "users": [ ❔ Get website icons from URLHey, so I'm currently learning C# (i have coded in other languages already) and for my first project❔ Serilog Extend LogLevels or Intercept LogsI am using Serilog and I have a specific use case right. I got three sinks (Console, File, SQLServer❔ [Resolved] How to delete the OBJ folder after a buildHello it's been several days that when I try to build it gives me this error: ```csharp Fody: An ❔ Can you create .net core Web API with c# dev kit in vscode that DOESN'T use the minimal syntax?Seems like the option is not there like in VS.❔ AWS ALB with Lambda target group with dotnet 6 not getting multi value querystring parameters?Does anyone know all the places that need to be checked, to ensure multi value querystring parameter