[英]How do I tell EF Core about two properties of the same type?
I have a set of models representing legal cases.我有一组代表法律案件的模型。 One of the actions a user can do on a case is generate a document.用户可以对案例执行的操作之一是生成文档。 This action is saved as a History
entity, with an associated HistoryFile
entity that contains the data about the file.此操作被保存为一个History
实体,并带有一个关联的HistoryFile
实体,该实体包含有关文件的数据。 Other actions may result in a History
entity, with zero or multiple associated HistoryFile
entities.其他操作可能会导致一个History
实体,具有零个或多个关联的HistoryFile
实体。
Cut-down versions of these two classes looks like this...这两个类的缩减版看起来像这样......
public class History {
public int Id { get; set; }
public ObservableCollection<HistoryFile> HistoryFiles { get; set; }
}
public class HistoryFile {
public int Id { get; set; }
public int HistoryId { get; set; }
public History History { get; set; }
}
The next requirement is that a user can pick up on a document that was previously generated and continue working on it.下一个要求是用户可以选择先前生成的文档并继续处理它。 The bit where I'm getting stuck is that the HistoryFile
entity needs a reference back to the History
entity that held the previous version.我卡住的一点是HistoryFile
实体需要引用回包含先前版本的History
实体。 This means that I need to add two lines of code to the HistoryFile
entity...这意味着我需要在HistoryFile
实体中添加两行代码......
public class HistoryFile {
public int Id { get; set; }
public int HistoryId { get; set; }
public History History { get; set; }
public int? PreviousHistoryId { get; set; }
public virtual History PreviousHistory { get; set; }
}
This means that there are two links from a HistoryFile
to a History
, one required one which is the parent History
entity (via the History
property) and an optional one via the PreviousHistory
property.这意味着从HistoryFile
到History
有两个链接,一个是必需的,它是父History
实体(通过History
属性),另一个是通过PreviousHistory
属性的可选链接。
I can't work out how to set this up for EF Core.我不知道如何为 EF Core 进行设置。 As the code stands now, when I try to add a migration, I get the following error...按照现在的代码,当我尝试添加迁移时,出现以下错误...
Cannot create a relationship between 'History.HistoryFiles' and 'HistoryFile.PreviousHistory' because a relationship already exists between 'History.HistoryFiles' and 'HistoryFile.History'.无法在“History.HistoryFiles”和“HistoryFile.PreviousHistory”之间创建关系,因为“History.HistoryFiles”和“HistoryFile.History”之间已经存在关系。 Navigation properties can only participate in a single relationship.导航属性只能参与单个关系。 If you want to override an existing relationship call 'Ignore' on the navigation 'HistoryFile.PreviousHistory' first in 'OnModelCreating'.如果您想在“OnModelCreating”中首先在导航“HistoryFile.PreviousHistory”上覆盖现有关系调用“Ignore”。
I tried adding the following to my DbContext
...我尝试将以下内容添加到我的DbContext
...
builder.Entity<HistoryFile>(entity => {
entity.HasOne(hf => hf.History)
.WithMany(h => h.HistoryFiles)
.HasForeignKey(hf => hf.HistoryId)
.OnDelete(DeleteBehavior.Restrict);
entity.HasOne(hf => hf.PreviousHistory)
.WithMany(h => h.HistoryFiles)
.HasForeignKey(hf => hf.PreviousHistoryId)
.OnDelete(DeleteBehavior.Restrict);
});
...but it didn't make any difference. ......但它没有任何区别。
Anyone able to tell me how I configure this so that EF Core knows that there are two distinct links between the two entities?谁能告诉我如何配置它,以便 EF Core 知道两个实体之间有两个不同的链接?
I'm using EF Core 5.0.7 in a .NET5 project in case it makes a difference.我在 .NET5 项目中使用 EF Core 5.0.7,以防万一。
Thanks谢谢
Got it.知道了。
I needed to add the following two lines to the History
class...我需要将以下两行添加到History
class ...
public virtual ICollection<HistoryFile> HistoryFilesParentHistory { get; set; }
public virtual ICollection<HistoryFile> HistoryFilesPreviousHistory { get; set; }
...and then change the code I added to the DbContext
to look like this... ...然后将我添加到DbContext
的代码更改为如下所示...
builder.Entity<HistoryFile>(entity => {
entity.HasOne(hf => hf.History)
.WithMany(h => h.HistoryFilesParentHistory)
.HasForeignKey(hf => hf.HistoryId)
.OnDelete(DeleteBehavior.Restrict);
entity.HasOne(hf => hf.PreviousHistory)
.WithMany(h => h.HistoryFilesPreviousHistory)
.HasForeignKey(hf => hf.PreviousHistoryId)
.OnDelete(DeleteBehavior.Restrict);
});
This worked fine.这工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.