[英]EFCore - How to have multiple navigation properties to the same type?
我的模型包含Post和PostHistory類,其中Post與PostHistory有一對多的關系。
class Post
{
public int Id { get; set; }
public PostVersion CurrentVersion { get; set; }
public PostVersion OriginalVersion { get; set; }
public IList<PostVersion> History { get; set; }
}
class PostVersion
{
public int Id { get; set; }
public Post Post { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
History屬性包含與該Post相關的所有PostVersions的列表。 CurrentVersion和PreviousVersion屬性都引用該帖子歷史記錄中的特定版本(最新版本和最新版本)。
我的問題是EF Core很難理解由於CurrentVersion和OriginalVersion導航屬性導致的關系。 當我嘗試創建遷移時,我收到以下錯誤消息:
Unable to determine the relationship represented by navigation property 'Post.CurrentVersion' of type 'PostVersion'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
之后,我嘗試使用Fluent API手動創建關系。
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Post>()
.HasMany(x => x.History)
.WithOne(x => x.Post);
builder.Entity<Post>()
.HasOne(x => x.CurrentVersion)
.WithOne(x => x.Post);
builder.Entity<Post>()
.HasOne(x => x.OriginalVersion)
.WithOne(x => x.Post);
}
但創造了一個不同的錯誤:
Cannot create a relationship between 'PostVersion.Post' and 'Post.CurrentVersion', because there already is a relationship between 'Post.History' and 'PostVersion.Post'. Navigation properties can only participate in a single relationship.
是否有可能在EF Core代碼中首先創建這種關系?
編輯我做了一些更改,你不能在多個關系中引用相同的屬性。 因此我不得不使用外鍵進行映射。 PostVersion只有你需要的一個Post引用。
public class Post
{
public Guid Id { get; set; }
public Guid CurrentVersionId { get; set; }
public PostVersion CurrentVersion { get; set; }
public Guid OriginalVersionId { get; set; }
public PostVersion OriginalVersion { get; set; }
public IList<PostVersion> History { get; set; }
}
public class PostVersion
{
public Guid Id { get; set; }
public Guid PostId { get; set; }
public Post Post { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
modelBuilder.Entity<Post>()
.HasOne(x => x.CurrentVersion)
.WithOne()
.HasForeignKey<Post>(p => p.CurrentVersionId);
modelBuilder.Entity<Post>()
.HasOne(x => x.OriginalVersion)
.WithOne()
.HasForeignKey<Post>(p => p.OriginalVersionId);
modelBuilder.Entity<Post>()
.HasMany(x => x.History)
.WithOne(p => p.Post)
.HasForeignKey(pv => pv.PostId);
原始您需要為第二個關系指定其他屬性
public class Post
{
public Guid Id { get; set; }
public PostVersion CurrentVersion { get; set; }
public PostVersion OriginalVersion { get; set; }
public IList<PostVersion> History { get; set; }
}
public class PostVersion
{
public Guid Id { get; set; }
public Post Post { get; set; }
public Post SecondPost { get; set; }
public Post ThirdPost { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
modelBuilder.Entity<Post>()
.HasOne(x => x.CurrentVersion)
.WithOne(x => x.Post);
modelBuilder.Entity<Post>()
.HasOne(x => x.OriginalVersion)
.WithOne(x => x.SecondPost);
modelBuilder.Entity<Post>()
.HasMany(x => x.History)
.WithOne(x => x.ThirdPost);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.