I want to make use of the new many-to-many feature in ef core 5.0. https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key
My existing classes are
public class User : IdentityUser<int>
{
}
and
public class Notification
{
public int ID { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public User Creator { get; set; }
public User Updater { get; set; }
public Notification()
{
}
}
I want to add a many-to-many relation between them so the classes are
public class User : IdentityUser<int>
{
public ICollection<Notification> Notifications { get; set; }
}
and
public class Notification
{
//[...]
public ICollection<User> Recipients { get; set; }
}
Now dotnet ef migrations add NotificationRecipients
results in Unable to determine the relationship represented by navigation 'Notification.Recipients' of type 'ICollection<User>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
After some research I found the [InverseProperty()]
annotation so I added it to Notification.cs
public class Notification
{
//[...]
[InverseProperty("Notifications")]
public ICollection<User> Recipients { get; set; }
}
The many-to-many now seems to be resolved but dotnet ef migrations add NotificationRecipients
results in Unable to determine the relationship represented by navigation 'Notification.Creator' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Is there any Annotation I can add to Notification.Creator
and Notification.Updater
or any other way to define the one-to-many relations between User
and Notification
?
I am using MSSQL as database.