【问题标题】:'Cannot insert the value NULL' error is showing when trying to empty a collection尝试清空集合时显示“无法插入值 NULL”错误
【发布时间】:2017-07-17 08:43:07
【问题描述】:

我正在使用实体框架。 ChatRoom 类有一个 TranscriptDownloadUserInfo 类的列表。现在,当我尝试清空时,正在使用 .Clear(),它显示“无法插入值 NULL”;

ChatRoom.cs

    public class ChatRoom
    {
        [Key]
        public int Key { get; set; }

        public virtual ICollection<TranscriptDownloadUserInfo> TranscriptDownAllowedUsers { get; set; }

        public ChatRoom()
        {
            TranscriptDownAllowedUsers = new SafeCollection<TranscriptDownloadUserInfo>();
        }
    }

TranscriptDownloadUserInfo.cs

    public class TranscriptDownloadUserInfo
    {
        [Key]
        public int Key { get; set; }

        public virtual ChatUser User { get; set; }
        public int? UserKey { get; set; }

        public bool Allowed { get; set; }
    }

TranscriptDownloadUserInfo 映射

    public TranscriptDownloadUserInfoMap()
    {
        this.HasKey(t => t.Key);

        this.ToTable("TranscriptDownAllowedUsers");
        this.Property(t => t.Key).HasColumnName("Key");
        this.Property(t => t.UserKey).HasColumnName("UserKey");
        this.Property(t => t.Allowed).HasColumnName("Allowed");

        this.HasRequired(t => t.User)
            .WithMany()
            .HasForeignKey(t => t.UserKey);
    }

添加-迁移代码

    public override void Up()
    {
        CreateTable(
            "dbo.TranscriptDownAllowedUsers",
            c => new
            {
                Key = c.Int(nullable: false, identity: true),
                UserKey = c.Int(nullable: false),
                Allowed = c.Boolean(nullable: false, defaultValue: false),
                ChatRoom_Key = c.Int(nullable: false),
            })
            .PrimaryKey(t => t.Key)
            .ForeignKey("dbo.ChatUsers", t => t.UserKey, cascadeDelete: true)
            .ForeignKey("dbo.ChatRooms", t => t.ChatRoom_Key)
            .Index(t => t.UserKey)
            .Index(t => t.ChatRoom_Key);
    }

产生错误的代码

room.TranscriptDownAllowedUsers.Clear();
_repository.CommitChanges();

内部异常

无法将值 NULL 插入表中的“ChatRoom_Key”列 'Dabb.dbo.TranscriptDownAllowedUsers';列不允许空值。 UPDATE 失败。\r\n语句已终止。

我不确定为什么会发生此错误。任何建议都将受到高度赞赏。

【问题讨论】:

标签: c# sql-server database entity-framework


【解决方案1】:

您的字段不可为空。

            Key = c.Int(nullable: false, identity: true),
            UserKey = c.Int(nullable: false),
            Allowed = c.Boolean(nullable: false, defaultValue: false),
            ChatRoom_Key = c.Int(nullable: false),

您可以插入默认值或其他内容,但不能插入任何内容。

【讨论】:

  • 通过执行 .clear(),我想删除该房间的所有 TranscriptDownAllowedUsers 记录,我不想将列值设置为空。有什么建议吗?
  • 我假设该聊天室中每个用户的所有聊天室密钥都相同?
  • 会有多个聊天室和多个聊天用户
  • 我想有一个.empty?快速搜索,也许这会有所帮助。 stackoverflow.com/questions/1969993/…
  • 该错误来自作为实体框架的数据库,而不是执行“从 ChatRoom_Key='111' 的 Dabb.dbo.TranscriptDownAllowedUsers 中删除”这个查询,它执行“更新 Dabb.dbo.TranscriptDownAllowedUsers 设置 ChatRoom_Key= NULL where ChatRoom_Key='111'"... 我该如何解决这个问题?
【解决方案2】:

我认为 nullable: false, identity: true 存在矛盾,因为您将键列定义为标识,因此可以插入 EF assumes null 值以生成新键。

在这种情况下,您也许应该将其设为可空。

也就是说,你 must 使你的外键 ChatRoom_Key 可以为空,因为 Clear 必须删除引用。

【讨论】:

  • 好点。因为我不需要那个键列。让我尝试删除它。然后让我们看看会发生什么。
  • 我还更新了我的答案,发现另一个问题:外键必须为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2023-03-16
相关资源
最近更新 更多