© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
119 replies
S-IERRA

❔ EFC Is not loading collection properly

I have the following 2 models in my database, EFC does not seem to be loading it at all for some reason

public record ChatUser : IEntityTypeConfiguration<ChatUser>
{
    public Guid Id { get; set; }
    
    public required string Username { get; set; }
    public required string Email { get; set; }
    public required string HashedPassword { get; set; }
    
    public Guid? RegistrationToken { get; set; }
    public Guid? PasswordResetToken { get; set; }
    
    public virtual ICollection<ChatChannel> Channels { get; set; } = new HashSet<ChatChannel>();
    public virtual ICollection<ChatRelationship> Relationships { get; set; } = new HashSet<ChatRelationship>();

    public void Configure(EntityTypeBuilder<ChatUser> builder)
    {
        builder.Property(x => x.Id)
            .HasDefaultValue(Guid.NewGuid());
    }
}


public record ChatRelationship : IEntityTypeConfiguration<ChatRelationship>
{
    public Guid Id { get; set; }
    
    public required Guid SelfId { get; set; }
    public ChatUser Self { get; set; }
    
    public required Guid OtherUserId { get; set; }
    public ChatUser OtherUser { get; set; }

    public required ChatRelationShipType Type { get; set; } = ChatRelationShipType.None;

    public void Configure(EntityTypeBuilder<ChatRelationship> builder)
    {
        builder.Property(x => x.Id)
            .HasDefaultValue(Guid.NewGuid());
        
        builder.HasKey(f => new { User1Id = f.SelfId, User2Id = f.OtherUserId });

        builder.HasOne(x => x.Self)
            .WithMany(x => x.Relationships)
            .HasForeignKey(x => x.SelfId)
            .OnDelete(DeleteBehavior.Restrict);
        
        builder.HasOne(x => x.OtherUser)
            .WithMany()
            .HasForeignKey(x => x.OtherUserId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}
public record ChatUser : IEntityTypeConfiguration<ChatUser>
{
    public Guid Id { get; set; }
    
    public required string Username { get; set; }
    public required string Email { get; set; }
    public required string HashedPassword { get; set; }
    
    public Guid? RegistrationToken { get; set; }
    public Guid? PasswordResetToken { get; set; }
    
    public virtual ICollection<ChatChannel> Channels { get; set; } = new HashSet<ChatChannel>();
    public virtual ICollection<ChatRelationship> Relationships { get; set; } = new HashSet<ChatRelationship>();

    public void Configure(EntityTypeBuilder<ChatUser> builder)
    {
        builder.Property(x => x.Id)
            .HasDefaultValue(Guid.NewGuid());
    }
}


public record ChatRelationship : IEntityTypeConfiguration<ChatRelationship>
{
    public Guid Id { get; set; }
    
    public required Guid SelfId { get; set; }
    public ChatUser Self { get; set; }
    
    public required Guid OtherUserId { get; set; }
    public ChatUser OtherUser { get; set; }

    public required ChatRelationShipType Type { get; set; } = ChatRelationShipType.None;

    public void Configure(EntityTypeBuilder<ChatRelationship> builder)
    {
        builder.Property(x => x.Id)
            .HasDefaultValue(Guid.NewGuid());
        
        builder.HasKey(f => new { User1Id = f.SelfId, User2Id = f.OtherUserId });

        builder.HasOne(x => x.Self)
            .WithMany(x => x.Relationships)
            .HasForeignKey(x => x.SelfId)
            .OnDelete(DeleteBehavior.Restrict);
        
        builder.HasOne(x => x.OtherUser)
            .WithMany()
            .HasForeignKey(x => x.OtherUserId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}
image.png
image.png
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
Next page

Similar Threads

Blazor CSS not loading properly
C#CC# / help
5mo ago
EFC -> Nested Property Sorting
C#CC# / help
2y ago
.Net 7 EFC initial migration issue
C#CC# / help
4y ago
❔ Role Based Authorization is not working properly.
C#CC# / help
3y ago