3

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.

3
  • I Cannot understand you made a many to many relation between user and notification and in the same time you need to make one to many relation between user and notification also !!! it's not doable you can add CreatorId and UpdaterId and add [NotMapped] to Creator property and Updater Jan 18, 2021 at 19:35
  • I am pretty sure its Possible. The relations have different purposes and Creator and Updater have been added in previous migrations. CreatorId and UpdaterId are already DBColumns. Jan 18, 2021 at 19:51
  • i mean it's not doable in this way, try to add creatorId and make creator NotMapped or manual configuration using fluent api as Ivan's answer Jan 18, 2021 at 20:21

1 Answer 1

3

Multiple relationships between two entities usually cannot be determined automatically and need configuration. Many things (and especially relationships related) in EF Core can only be configured only with fluent API.

So rather than looking for non existing or non intuitive data annotations, simply configure the desired relationships (at minimum - navigations properties and cardinality) fluently:

modelBuilder.Entity<Notification>(builder =>
{
    builder.HasOne(e => e.Creator).WithMany();
    builder.HasOne(e => e.Updater).WithMany();
    builder.HasMany(e => e.Recipients).WithMany(e => e.Notifications);
});
2
  • Probably you ment builder.Entity<Notification>().*? If I try this then I am getting The entity type 'IdentityUserLogin<int>' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943. when I try to create my migration. Jan 18, 2021 at 20:02
  • If you inherit IdentityDbCiontext as it seems, make sure to call base.OnModelCreating(modelBuilder); at the beginning of your OnModelCreating override. If that doesn't help, then you have another separate issue - EF Core reports the first one found, and when it is fixed - the next etc. This code should have fixed the relationship issue from your question. For other issues - new question.
    – Ivan Stoev
    Jan 18, 2021 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.