C
C#2y ago
reeeeeee

❔ EF modelbuilder for table without primary keys

I have a simple table JobUnitTag with two fields, Guid JobUnit and Guid JobTag. I tried to build it like this modelBuilder.Entity<JobUnitTag>(), but it does not work because it requires primary key. I tried modelBuilder.Entity<PlanJobUnitTag>().HasNoKey(); and the error was the same. Then i gave my JobUnit a [Key] identifier (even tho its not primary key), and it works. However, I occur a problem when I try to insert a new entry with the same JobUnit value. (I assume its because of the [Key]. Any idea how could I bypass that?
3 Replies
Angius
Angius2y ago
Give it a composite key It's a join table, so that's the best way to solve it
.HasKey(x => new { x.JobUnit, x.JobTag })
.HasKey(x => new { x.JobUnit, x.JobTag })
reeeeeee
reeeeeee2y ago
Thanks, this does the job!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.