简体   繁体   English

如何告诉 EF Core 相同类型的两个属性?

[英]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.这意味着从HistoryFileHistory有两个链接,一个是必需的,它是父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.

相关问题 如何告诉EF核心两个型号使用同一个表? - How to tell EF core two models use the same table? 具有相同类型导航属性集合的EF6代码第一实体-如何告诉EF是什么关系? - EF6 Code First Entity with navigation property collection of same type - how do I tell EF what the relationship is? 如何更新 EF Core 中的特定属性? - How do I update the specific properties in EF Core? 如何判断 EF Core 实体上的哪些属性已更改? - How to tell which properties have changed on an EF Core Entity? EF 5-6.1.1中的NullReferenceException,具有两个相同类型的导航属性 - NullReferenceException in EF 5-6.1.1 with two navigation properties to the same type 如何在 EF.Core 5 中添加具有相同外键的多个导航属性? - How can I add multiple navigation properties with the same Foreign Key in EF.Core 5? EF Core 3.0.0 - 两个相同类型的一对一关系 - EF Core 3.0.0 - Two one-to-one relations of same type EF Core,如何将 map 两个实体放在不同的属性上 - EF Core, how to map two entities on different properties EF Core 2、.NET CORE 2:如何使用 IQueryable 查询同一列的多个条件<t> ?</t> - EF Core 2, .NET CORE 2 :How do I query the same column for multiple conditions using IQueryable<T>? 如何使用EF Core加强模型属性之间的或非关系? - How do I enforce an either-or relationship between model properties, using EF Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM