首页 > 解决方案 > EFCORE 将值添加到一列会导致它被添加到不相关的列

问题描述

在此处输入图像描述

在这张图片中,我有三个相关的列,AllowedInvitesFilteredWordsFilteredRegexes。它们都是 type List<string>。我正在调用这个方法。

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();
}

这应该只添加wordsFilteredWords列中。但是,根据屏幕截图,它也被添加到其他两列。

如果我尝试删除,它会按预期工作。此问题仅在添加时发生。

我正在使用 Postgres。

请帮助我傻眼了。

Guild班级

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();
        }
    }
}

标签: c#databasepostgresqlentity-framework-core

解决方案


推荐阅读