Reputation: 13
I am currently developing a Blazor app that uses EntityFrameworkCore, for the given code :
public class Movie
{
public string title { get; set; }
public string language { get; set; }
public HashSet<Staff> staffs { get; set; }
public HashSet<Tag> tags { get; set; }
public float averageRating { get; set; }
public string MovieID { get; set; }
public Movie(string title, string language, string movieID)
{
this.title = title;
this.language = language;
staffs = new HashSet<Staff>();
tags = new HashSet<Tag>();
averageRating = 0;
this.MovieID = MovieID;
}
}
public class Staff
{
public string fullName { get; set; }
public HashSet<Movie> isActor { get; set; }
public HashSet<Movie> isDirector { get; set; }
public string StaffID { get; set; }
public Staff(string fullName, string staffID)
{
this.fullName = fullName;
isActor = new HashSet<Movie>();
isDirector = new HashSet<Movie>();
this.StaffID = staffID;
}
}
public class Tag
{
public string name { get; set; }
public HashSet<Movie> movies { get; set; }
public string TagID { get; set; }
public Tag(string name, string tagID)
{
this.name = name;
movies = new HashSet<Movie>();
this.TagID = tagID;
}
}
I am using the code-first method. And I need to work with the database and first of all, I wanted to cure it, so when I used Add-Migration I am getting an error:
Unable to determine the relationship represented by navigation 'Movie.staffs' of type 'HashSet'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Upvotes: 1
Views: 5779
Reputation: 706
The Staff class has two collections of Movies, so the Movie class does not know if the staffs
are actors or directors.
So you need to define two collections of Staff on the Movie:
public HashSet<Staff> actors { get; set; }
public HashSet<Staff> directors { get; set; }
But EF does not know if actors
should pair with isActor
or isDirector
.
From MSDN:
When there are multiple navigation properties defined between two types (that is, more than just one pair of navigations that point to each other) the relationships represented by the navigation properties are ambiguous. You will need to manually configure them to resolve the ambiguity.
So you need to pair these manually:
modelBuilder
.Entity<Movie>()
.HasMany(p => p.actors)
.WithMany(p => p.isActor);
modelBuilder
.Entity<Movie>()
.HasMany(p => p.directors)
.WithMany(p => p.isDirector);
Upvotes: 3
Reputation: 4634
You need to add a MovieID
property (foreign key) to your Staff
class.
Upvotes: 0