© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
null

[EF Core 8] Many-to-many relationship with payload

Hi all,
I am trying to create a many-to-many relationship as described in https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-and-join-table-with-payload
My models are the following:
public class Post
{
    public int Id { get; set; }
    public List<Tag> Tags { get; } = new();
    public List<PostTag> PostTags { get; } = new();
}

public class Tag
{
    public int Id { get; set; }
    public List<Post> Posts { get; } = new();
    public List<PostTag> PostTags { get; } = new();
}

public class PostTag
{
    public int PostId { get; set; }
    public int TagId { get; set; }
    public string CustomPayload { get; set; }
}
public class Post
{
    public int Id { get; set; }
    public List<Tag> Tags { get; } = new();
    public List<PostTag> PostTags { get; } = new();
}

public class Tag
{
    public int Id { get; set; }
    public List<Post> Posts { get; } = new();
    public List<PostTag> PostTags { get; } = new();
}

public class PostTag
{
    public int PostId { get; set; }
    public int TagId { get; set; }
    public string CustomPayload { get; set; }
}

MyDbContext:
public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
    public virtual DbSet<Post> Posts { get; set; }
    public virtual DbSet<Tag> Tags { get; set; }
}
public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
    public virtual DbSet<Post> Posts { get; set; }
    public virtual DbSet<Tag> Tags { get; set; }
}

when I try to create the initial migration I get the following error:
Unable to create a 'DbContext' of type ''. The exception 'The entity type 'PostTag' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'
Unable to create a 'DbContext' of type ''. The exception 'The entity type 'PostTag' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'

When adding a primary key on the
PostTag
PostTag
model:
Unable to create a 'DbContext' of type ''. The exception 'Cannot use table 'PostTag' for entity type 'PostTag' since it is being used for entity type 'PostTag (Dictionary<string, object>)' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'PostTag' on the primary key properties and pointing to the primary key on another entity type mapped to 'PostTag'.
Unable to create a 'DbContext' of type ''. The exception 'Cannot use table 'PostTag' for entity type 'PostTag' since it is being used for entity type 'PostTag (Dictionary<string, object>)' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'PostTag' on the primary key properties and pointing to the primary key on another entity type mapped to 'PostTag'.

I have a few questions:

1) What am I missing and cannot make this relationship function?
2) Even if somehow the tables are produced from the migration, will the
PostTag
PostTag
table be able to return the
CustomPayload 
CustomPayload 
and how?
3) Is this supposed to be the correct way to supply the join table with a
user defined payload
user defined payload
?
Many-to-many relationships - EF Core
How to configure many-to-many relationships between entity types when using Entity Framework Core
Many-to-many relationships - EF Core
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

EF Core Many to Many relationship through Entity
C#CC# / help
14mo ago
✅ Inserting record with one-to-many relationship EF Core
C#CC# / help
3y ago
✅ EF Core 8.0.3 Many To Many Relation
C#CC# / help
2y ago
✅ EF Core Relationship
C#CC# / help
2y ago