Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EFCore - How to have multiple navigation properties to the same type?

My model contains the classes Post and PostHistory, where Post has a one-to-many relationship with 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; }
}

The History property contains a list of all PostVersions related to that Post. The CurrentVersion and PreviousVersion properties both reference a perticlar version in that post history (most likley the most recent version and the first version).

My problem is that EF Core struggles to understand the relationship due to the CurrentVersion and OriginalVersion navigation properties. When I try to create a migration, I get this error message:

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'.

After that I tried to use the Fluent API to create the relationships manually.

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);
}

But created a different error:

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.

Is it possible to create this kind of relationship in EF Core code-first?

like image 873
Alex Avatar asked Dec 15 '18 12:12

Alex


People also ask

What is navigation properties in EDMX?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

How do you create a many-to-many relationship in EF core?

Many-to-many relationships require a collection navigation property on both sides. They will be discovered by convention like other types of relationships. The way this relationship is implemented in the database is by a join table that contains foreign keys to both Post and Tag .

How does Entity Framework handle many-to-many relationships?

To configure many-to-many relationship Using Data Annotations, you need to create the Join Table in the model. The Join Table BookCategory will have properties for the primary key of both the table. It will have two navigational properties one each for Book and Category class.

What is a principal key?

Key Principal means the natural person(s) or entity identified as such at the foot of this Instrument, and any person or entity who becomes a Key Principal after the date of this Instrument and is identified as such in an amendment or supplement to this Instrument.


1 Answers

Edit I did some changes, you cannot reference same property in multiple relationships. Therefore i had to use foreign keys for mappings. PostVersion has only one Post reference as you required.

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);

Original You need to specify additional property for that second relationship

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);
like image 130
Pribina Avatar answered Oct 23 '22 08:10

Pribina