C# 为什么我会收到“关系无法更改”例外? (EF)

发布于 2024-12-10 13:58:01 字数 1508 浏览 0 评论 0原文

我有这个方法:

    public void AddFile(MediaFileName mediaFile)
    {
        // Get a list of items which must be removed
        var tmp = MediaFileNames.ToList().Where(m => m.FileName.Contains(mediaFile.FileType.ToString())).ToList(); // FileType is an enum

        // remove each item from the Navigation property from memory
        tmp.ForEach(f => MediaFileNames.Remove(f));

        // Store items with an id in a list. This list is accessed by the presenter to delete these records
        tmp.Where(f => f.Id != 0).ToList().ForEach(f => _filesToRemove.Add(f));

        // Set the items filename
        mediaFile.FileName = mediaFile.FileType.ToString() + new FileInfo(mediaFile.SourceFile).Extension;

        // Add the item to the navigation property 
        MediaFileNames.Add(mediaFile);
    }

MediaFilesNames 是 Media 类的导航属性。

我保留了一个必须从数据库中删除的项目列表(_filesToRemove)。从我的存储库访问此列表:

    public bool Update(Act act)
    {
        foreach (var file in act.Media.FilesToRemove)
        {
            if (_context.MediaFileNames.FirstOrDefault(f => file.Id == f.Id) != null)
                _context.MediaFileNames.DeleteObject(file);
        }

        _context.SaveChanges();

        return true;
    }

调用 SaveChanges 时,我收到以下消息:

操作失败:无法更改关系,因为一个或多个外键属性不可为空。当关系发生更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

我不明白为什么我收到此消息是因为我正在删除现有项目并且仅添加一项新项目。我希望你能帮助我。

I have this method:

    public void AddFile(MediaFileName mediaFile)
    {
        // Get a list of items which must be removed
        var tmp = MediaFileNames.ToList().Where(m => m.FileName.Contains(mediaFile.FileType.ToString())).ToList(); // FileType is an enum

        // remove each item from the Navigation property from memory
        tmp.ForEach(f => MediaFileNames.Remove(f));

        // Store items with an id in a list. This list is accessed by the presenter to delete these records
        tmp.Where(f => f.Id != 0).ToList().ForEach(f => _filesToRemove.Add(f));

        // Set the items filename
        mediaFile.FileName = mediaFile.FileType.ToString() + new FileInfo(mediaFile.SourceFile).Extension;

        // Add the item to the navigation property 
        MediaFileNames.Add(mediaFile);
    }

MediaFilesNames is a navigation property of the class Media.

I keep a list (_filesToRemove) of items which has to be removed from the database. This list is accessed from my repository:

    public bool Update(Act act)
    {
        foreach (var file in act.Media.FilesToRemove)
        {
            if (_context.MediaFileNames.FirstOrDefault(f => file.Id == f.Id) != null)
                _context.MediaFileNames.DeleteObject(file);
        }

        _context.SaveChanges();

        return true;
    }

When SaveChanges is called I get this message:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

I don't get why I get this message, because I am deleting existing items and I am only adding one new item. I hope you can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦 2024-12-17 13:58:01

尝试检查数据库中的关系,并指定它们的删除规则必须以级联模式完成。

或者,尝试检查相关表中的外键,并将其 ALLOW NULL 值设置为 true。您可以做的是对相关列使用索引并对其添加唯一约束,以便您可以将它们用作关系中的外键。

如果该列是主键的一部分,则无法将 ALLOW NULL 设置为 true。

Try to check the relationships in your database, and specify that their delete rules must be done in cascade mode.

Or, try to check your foreign keys in the tables that are concerned, and set their ALLOW NULL value to true. What you can do is use indexes for the incriminated columns and add unique constraint on them, so that you can use them as foreign key in your relationships.

If the column is part of the primary key, you won't be able to set ALLOW NULL to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文