I have a Movie class that has lists of Actors and Directors.
Whenever I'm creating a Movie (using a Rest API) that uses those, everything's fine.
But when I'm creating a second one with an already existing Actor / Director, my first Movie loses its reference to said Actor/Director.
How can I fix this so that it doesn't do that? Should I create a new class that references Movies and Actors/Directors? How should I do that?
Here's my class in case anything catches the eye.
public class Movie
{
public int Id { get; set; }
public required string Title { get; set; }
public required List<Actor> Actors { get; set; }
public required List<Director> Directors { get; set; }
}
I'm using Entity Framework's InMemory database system.
Everything else seems to be working correctly. The Movie references Actors and Directors, but not the other way around, I would like to avoid that.
You should really provide the models for
ActorandDirectoras well but, based on the behavior, I assume you set up 1:N relationships fromActor|DirectortoMovie, so eachActorandDirectorcan only associate with a singleMovie. The relationship should be set up as many-to-many as a film can have multiple actors/directors and actors/directors can have multiple films, soActor|Directorshould also have aList<Movie> Moviesproperty instead ofMovie Movieproperties and/or the fluent config should be set up appropriately:https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many