[英]EF Core multiple references to same table with circullar reference
我瀏覽了相關的帖子,但我仍然無法弄清楚我的問題應該是什么正確的解決方案。 我正在為用戶消息傳遞應用程序創建數據庫結構。 我對架構的想法如下:用戶有很多對話和消息對話有 2 個用戶和很多消息消息有 1 個用戶和 1 個對話
我有 3 張桌子:
public class UserEntity : IEntity
{
public Guid Id { get; set; }
public ICollection<ConversationEntity> Conversations { get; set; }
public ICollection<MessageEntity> Messages { get; set; }
}
public class MessageEntity : IEntity
{
public Guid Id { get; set; }
public string Content { get; set; }
public Guid UserId { get; set; }
public UserEntity User { get; set; }
public Guid ConversationId { get; set; }
public ConversationEntity Conversation { get; set; }
}
public class ConversationEntity : IEntity
{
public Guid Id { get; set; }
public Guid UserOneId { get; set; }
public UserEntity UserOne { get; set; }
public Guid UserTwoId { get; set; }
public UserEntity UserTwo { get; set; }
public ICollection<MessageEntity> Messages { get; set; }
}
這就是我配置這些表的方式:
public void Configure(EntityTypeBuilder<UserEntity> builder)
{
builder.ToTable("Users");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedOnAdd();
}
public void Configure(EntityTypeBuilder<MessageEntity> builder)
{
builder.ToTable("Messages");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedOnAdd();
builder.HasOne(x => x.Conversation)
.WithMany(x => x.Messages)
.HasForeignKey(x => x.ConversationId);
builder.HasOne(x => x.User)
.WithMany(x => x.Messages)
.HasForeignKey(x => x.UserId);
}
public void Configure(EntityTypeBuilder<ConversationEntity> builder)
{
builder.ToTable("Conversations");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedOnAdd();
builder.HasOne(x => x.UserOne)
.WithMany()
.HasForeignKey(x => x.UserOneId)
.OnDelete(DeleteBehavior.NoAction);
builder.HasOne(x => x.UserTwo)
.WithMany()
.HasForeignKey(x => x.UserTwoId)
.OnDelete(DeleteBehavior.NoAction);
}
現在我有兩個問題:
在表“Conversations”上引入 FOREIGN KEY 約束“FK_Conversations_Users_UserTwoId”可能會導致循環或多個級聯路徑。 指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 約束。
我可以請幫助如何回滾嗎? 我嘗試應用較早的遷移,但這不起作用(在表 'Conversations' 上引入 FOREIGN KEY 約束 'FK_Conversations_Users_UserTwoId' 可能會導致循環或多個級聯路徑。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 約束。可以不創建約束。請參閱以前的錯誤。)
[編輯]:我已經測試了解決方案並重新創建了整個數據庫,有趣的是這個錯誤不再發生,但是在對話表的 Keys 文件夾中我可以看到:
為什么會有這個“FK_Conversations_Users_UserEntityId”??
For the second point if you are on .NET Frameework 4.x open Tools -> Nuget Package Manager -> Package Manager Console
under project tab select the project that contains the migrations folder and in the command line type Update-Database -TargetMigration:"NameOfMigrationYouWantToRollbackTo" -Force
。 If you use ASP.NET CORE/.NET Core open Tools -> Nuget Package Manager -> Package Manager Console
under the project tab select the project that contains the migrations folder and type Remove-Migration
to remove the last migration and after that Update-Database
to回滾數據庫。 如果要刪除兩個或多個遷移,只需重復該過程。
您可以通過將其作為常規的多對多關系來簡化用戶和對話之間的關系。 缺點是您需要強制執行每個對話必須有兩個用戶的規則。
為了盡量減少更改,可以編寫屬性來替換舊的UserOne
和UserTwo
屬性:
public UserEntity UserOne
{
get => Users[0];
set => Users[0] = value;
}
( Users
是添加到ConversationEntity
的新集合導航屬性)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.