C
C#

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

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

Nnull11/21/2023
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 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 table be able to return the CustomPayload and how? 3) Is this supposed to be the correct way to supply the join table with a user defined payload ?
AAngius11/21/2023
You might have to configure it I believe the errors are due to how EF now treats keyless entities as owned types So might need explicit relationship configured there Or at least a composite key on the join entity FWIW I have all my many-to-many with explicit join entities configured explicitly

Looking for more? Join the community!

C
C#

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

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
Do you know any C# code editors where I can run code?Under 500 MB✅ Getting back main functionRecently after updating to CS8 (didnt update in a long time), and creating a console app there is noCookie Clicker project for schoolso i have a problem i need to fix a bug on a picture. I use mouse down and mouse up events to resize✅ else statement executing regardless of whether I want it to or not, windows form appI've created a simple windows form app to create shapes on a bitmap, I have a function that gives anMaui adb additional parametersI need to test a Maui applicaiton on a physical android device. The app recorded a error, to see whHow to let compiler know that List is not null?Hi community! I have the following extension method:, which checks if the source is not null and if✅ How to Fix MSB6006 yasm.exe exited with code -1073741515 on Cosmos Kernel✅ Issue during pushing a commit to GitHub from VS CodeHi friends, I'm working on a project using `VS Code`, I used `VS Code` to clon that project from my✅ Need help understanding inheritance with constructorsI know about constructors partially (I'm used to Unity's MonoBehaviour so constructors are mostly suHow to get the data of an adapter using MQTTnetThis is the documentation: https://docs.service.sicon.eco/sicon.os/2023.19.x/adapter-protocol-smqtt Converting from a string to a generic method invocationLooking for design patterns to help solve this problem I’m having at work. The situation is that we Auto refresh inside windows formTeam , We are using windows form c#. Is there any library for auto refresh (need to few logic eve