简体   繁体   English

EF Core 5.0 添加多对多使得一对多无法确定

[英]EF Core 5.0 Adding many-to-many makes one-to-many unable to determine

I want to make use of the new many-to-many feature in ef core 5.0.我想利用 ef core 5.0 中新的多对多功能。 https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key https://docs.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'.现在dotnet ef migrations add NotificationRecipients导致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'. 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经过一番研究,我发现了[InverseProperty()]注释,所以我将它添加到 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'.多对多现在似乎已解决,但dotnet ef migrations add NotificationRecipients导致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'. 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 ?是否有任何注释可以添加到Notification.CreatorNotification.Updater或任何其他方式来定义UserNotification之间的一对多关系?

I am using MSSQL as database.我正在使用 MSSQL 作为数据库。

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. EF Core 中的许多东西(尤其是与关系相关的)只能使用流畅的 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM