1

enter image description here

In this image I have three relevant columns, AllowedInvites, FilteredWords, and FilteredRegexes. They are all of type List<string>. I am calling this method.

public async Task AddWordsToFilter(ulong guildId, params string[] words)
{
    var guild = await _kinaContext
        .Guilds
        .AsQueryable()
        .FirstOrDefaultAsync(x => x.GuildId == guildId);

    foreach (var word in words)
    {
        if (guild.FilteredWords.Contains(word))
        {
            continue; 
        }
        
        guild.FilteredWords.Add(word);
    }

    await _kinaContext.SaveChangesAsync();
}

This should only add the words to the FilteredWords column. However, as per the screenshot, it is added to the other two columns also.

If I try to remove, it works as expected. This issue only occurs on add.

I am using Postgres.

Please help I am dumbfounded.

Guild class

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Kinabot.Modules
{
    public class Guild
    {
        [Key]
        public int Id { get; set; }
        
        [Required]
        public ulong GuildId { get; init; }

        [Required]
        public ulong MessageLogChannelId { get; set; }

        [Required]
        public ulong InfractionLogChannelId { get; set; }

        [Required]
        public bool AllowsInvites { get; set; }
        
        public List<string> AllowedInvites { get; set; } = new();

        public bool Filter { get; set; }

        public List<string> FilteredWords { get; set; } = new();

        public List<string> FilteredRegexes { get; set; } = new(); 
         
        public ulong MuteRoleId { get; set; }
    }

    public class GuildConfiguration : IEntityTypeConfiguration<Guild>
    {
        public void Configure(EntityTypeBuilder<Guild> builder)
        {
            builder
                .Property(x => x.GuildId)
                .HasConversion<long>();
            
            builder
                .Property(x => x.MessageLogChannelId)
                .HasConversion<long>();
            
            builder
                .Property(x => x.InfractionLogChannelId)
                .HasConversion<long>();
            
            builder
                .Property(x => x.MuteRoleId)
                .HasConversion<long>();

            builder
                .HasIndex(x => x.GuildId)
                .IsUnique();
        }
    }
}
JensV
  • 3,997
  • 2
  • 19
  • 43
Matthew Trip
  • 326
  • 2
  • 15
  • Could you provide us the definition of your entity? – sebrojas Jan 06 '21 at 14:03
  • 1
    You mean for the Guilds table? – Matthew Trip Jan 06 '21 at 14:04
  • I have edited it in @sebrojas – Matthew Trip Jan 06 '21 at 14:08
  • What type does the column have in postgres? I'm not sure if you can simply add a `List` to your model. I feel like it would create a relation to `String` as an Entity and thus form a 1-n relationship. If they all use the primary key for identification, it would lead to the observed result – JensV Jan 06 '21 at 14:47
  • Does this answer your question? [How to persist a list of strings with Entity Framework Core?](https://stackoverflow.com/questions/37370476/how-to-persist-a-list-of-strings-with-entity-framework-core) – JensV Jan 06 '21 at 14:49
  • @JensV I'm pretty sure modern EF Core has supported primitive lists. I'm using them fine for some other functions and models. It's this that is a problem. – Matthew Trip Jan 06 '21 at 14:51
  • Do any of those other models use multiple lists of the same type? I'd be interested in the output of `\d+ GuildTableName` executed from a postgres shell. – JensV Jan 06 '21 at 14:56
  • Also see https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#limitations this would outline the experienced behaviour. Also from reading the page, using `List` is the primary method to define `1-n` relationships – JensV Jan 06 '21 at 15:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226931/discussion-between-matthew-trip-and-jensv). – Matthew Trip Jan 06 '21 at 15:06
  • 1
    @JensV This seems to be [unique feature of PostgreSQL provider](http://www.npgsql.org/efcore/mapping/array.html#mapping-arrays) – Ivan Stoev Jan 06 '21 at 15:30

0 Answers0