C
C#9mo ago
S-IERRA

❔ Entityframework model

I have the following models in EFC, I need the server member to share the id of user Id as I do not want to make a new Id for it entierly (if thats even possible) tho i'm getting errors saying the code needs a primary key when I do define a key
public class ChatServerMember : IEntityTypeConfiguration<ChatServerMember>
{
public required Guid UserId { get; set; }
public ChatUser User { get; set; }

public required Guid ServerId { get; set; }
public ChatServer Server { get; set; }

public ChatPermissions Permissions { get; set; }

public virtual ICollection<ChatServerRole> Roles { get; set; } = new HashSet<ChatServerRole>();

public void Configure(EntityTypeBuilder<ChatServerMember> builder)
{
builder.ToTable("ServerMembers");

builder.HasKey(x => new { x.UserId, x.ServerId });

builder.HasOne(x => x.Server)
.WithMany(x => x.Members)
.HasForeignKey(x => x.ServerId)
.OnDelete(DeleteBehavior.SetNull);
}
}
public class ChatServerMember : IEntityTypeConfiguration<ChatServerMember>
{
public required Guid UserId { get; set; }
public ChatUser User { get; set; }

public required Guid ServerId { get; set; }
public ChatServer Server { get; set; }

public ChatPermissions Permissions { get; set; }

public virtual ICollection<ChatServerRole> Roles { get; set; } = new HashSet<ChatServerRole>();

public void Configure(EntityTypeBuilder<ChatServerMember> builder)
{
builder.ToTable("ServerMembers");

builder.HasKey(x => new { x.UserId, x.ServerId });

builder.HasOne(x => x.Server)
.WithMany(x => x.Members)
.HasForeignKey(x => x.ServerId)
.OnDelete(DeleteBehavior.SetNull);
}
}
19 Replies
S-IERRA
S-IERRA9mo ago
maybe what I could do is have "Id" property thats just the value of UserId? tho that would be stupid
WEIRD FLEX
WEIRD FLEX9mo ago
i've not used composite keys often, but it's kinda weird to me that you're building an anonymous object
JakenVeina
JakenVeina9mo ago
a what?
WEIRD FLEX
WEIRD FLEX9mo ago
new { }
JakenVeina
JakenVeina9mo ago
ah, in the .HasKey() call
S-IERRA
S-IERRA9mo ago
Yea, just I'm receiving this error
InvalidOperationException: The entity type 'ChatServerMember' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
InvalidOperationException: The entity type 'ChatServerMember' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
which is strange
JakenVeina
JakenVeina9mo ago
that's the correct syntax for defining a composite key for EF
S-IERRA
S-IERRA9mo ago
oh is it
JakenVeina
JakenVeina9mo ago
yeah, it actually compiles to an expression, and EF parses it to see the 2 columns involved I'm gonna have to wager the configuration isn't getting applied
S-IERRA
S-IERRA9mo ago
most likley not If I could ask what the correct syntax would be then
JakenVeina
JakenVeina9mo ago
what do you have now?
S-IERRA
S-IERRA9mo ago
oh misread
S-IERRA
S-IERRA9mo ago
this
JakenVeina
JakenVeina9mo ago
no, for applying the entity configuration
S-IERRA
S-IERRA9mo ago
oh shit i forgot to add that
JakenVeina
JakenVeina9mo ago
😉
S-IERRA
S-IERRA9mo ago
I just noticed would you look at that it worked thanks, i didnt even think about that being the issue
JakenVeina
JakenVeina9mo ago
🎉
Accord
Accord9mo 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.