c# - EFCORE 将值添加到一列会导致它被添加到不相关的列
问题描述
在这张图片中,我有三个相关的列,AllowedInvites
、FilteredWords
和FilteredRegexes
。它们都是 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();
}
这应该只添加words
到FilteredWords
列中。但是,根据屏幕截图,它也被添加到其他两列。
如果我尝试删除,它会按预期工作。此问题仅在添加时发生。
我正在使用 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();
}
}
}
解决方案
推荐阅读
- highcharts - 高图上具有负值的 nGradient 效果
- tcl - TclOO:将“null”值分配给对象
- c# - 如何为 c# autocad 插件 + 自定义菜单创建安装程序
- python - 为什么即使我捕捉到异常,django 也会打印异常的回溯?
- loopback4 - Loopback4“待办事项”教程似乎只是部分工作
- python-2.7 - pyinstaller 和多个 tkinter 窗口的问题
- pdf-generation - DomPDF & 4 栏
- java - JMH - 错误:无法找到资源:/META-INF/BenchmarkList
- c# - 无法使用 Firebase 配置保存和加载
- docker - 码头同步问题:我正在运行 docker-sync start cmd,但出现错误