EF Core 5.0 加入多对多使得一对多无法确定
EF Core 5.0 Adding many-to-many makes one-to-many unable to determine
我想利用 ef core 5.0 中新的多对多功能。 https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key
我现有的类是
public class User : IdentityUser<int>
{
}
和
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()
{
}
}
我想在它们之间添加多对多关系,所以 类 是
public class User : IdentityUser<int>
{
public ICollection<Notification> Notifications { get; set; }
}
和
public class Notification
{
//[...]
public ICollection<User> Recipients { get; set; }
}
现在 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'.
经过一些研究,我找到了 [InverseProperty()]
注释,所以我将它添加到 Notification.cs
public class Notification
{
//[...]
[InverseProperty("Notifications")]
public ICollection<User> Recipients { get; set; }
}
现在似乎解决了多对多问题,但是 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'.
是否有任何注释可以添加到 Notification.Creator
和 Notification.Updater
或任何其他方式来定义 User
和 Notification
之间的一对多关系?
我正在使用 MSSQL 作为数据库。
两个实体之间的多重关系通常无法自动确定,需要配置。 EF Core 中的很多东西(尤其是关系相关的)只能用 fluent API.
配置
因此,无需寻找不存在或不直观的数据注释,只需流畅地配置所需的关系(至少 - 导航属性和基数):
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);
});
我想利用 ef core 5.0 中新的多对多功能。 https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key
我现有的类是
public class User : IdentityUser<int>
{
}
和
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()
{
}
}
我想在它们之间添加多对多关系,所以 类 是
public class User : IdentityUser<int>
{
public ICollection<Notification> Notifications { get; set; }
}
和
public class Notification
{
//[...]
public ICollection<User> Recipients { get; set; }
}
现在 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'.
经过一些研究,我找到了 [InverseProperty()]
注释,所以我将它添加到 Notification.cs
public class Notification
{
//[...]
[InverseProperty("Notifications")]
public ICollection<User> Recipients { get; set; }
}
现在似乎解决了多对多问题,但是 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'.
是否有任何注释可以添加到 Notification.Creator
和 Notification.Updater
或任何其他方式来定义 User
和 Notification
之间的一对多关系?
我正在使用 MSSQL 作为数据库。
两个实体之间的多重关系通常无法自动确定,需要配置。 EF Core 中的很多东西(尤其是关系相关的)只能用 fluent API.
配置因此,无需寻找不存在或不直观的数据注释,只需流畅地配置所需的关系(至少 - 导航属性和基数):
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);
});