C
C#15mo 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-IERRAOP15mo ago
maybe what I could do is have "Id" property thats just the value of UserId? tho that would be stupid
Omnissiah
Omnissiah15mo ago
i've not used composite keys often, but it's kinda weird to me that you're building an anonymous object
JakenVeina
JakenVeina15mo ago
a what?
Omnissiah
Omnissiah15mo ago
new { }
JakenVeina
JakenVeina15mo ago
ah, in the .HasKey() call
S-IERRA
S-IERRAOP15mo 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
JakenVeina15mo ago
that's the correct syntax for defining a composite key for EF
S-IERRA
S-IERRAOP15mo ago
oh is it
JakenVeina
JakenVeina15mo 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-IERRAOP15mo ago
most likley not If I could ask what the correct syntax would be then
JakenVeina
JakenVeina15mo ago
what do you have now?
S-IERRA
S-IERRAOP15mo ago
oh misread
S-IERRA
S-IERRAOP15mo ago
this
JakenVeina
JakenVeina15mo ago
no, for applying the entity configuration
S-IERRA
S-IERRAOP15mo ago
oh shit i forgot to add that
JakenVeina
JakenVeina15mo ago
😉
S-IERRA
S-IERRAOP15mo ago
I just noticed would you look at that it worked thanks, i didnt even think about that being the issue
JakenVeina
JakenVeina15mo ago
🎉
Accord
Accord15mo 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.
Want results from more Discord servers?
Add your server