繁体   English   中英

在多对多关系 EF4 中插入或更新问题

[英]Problem in Inserting or Updating in many to many relationship EF4

假设有3张桌子:

Category
-------------
CategoryID    int
Title         text


Admin
------------
AdminID       int
FullName      text


Permission
------------
CategoryID    int
 AdminID      int
 AllowAccess  bit

当我尝试更新对数据库的更改时,出现以下异常:

Unable to insert or update an entity because the principal end of the 'KiaNetModel.FK_Permissions_Admins' relationship is deleted.

为什么?

function 更新变更:

public static void SetPermissions(int[] cats, int userId, Entities context)
        {
            var premissions = from p in context.AdminPremissions where p.AdminID == userId select p;
            // Clear all premissions...
            foreach (var p in premissions)
            {
                p.AllowAccess = false;
            }

            foreach (var c in cats)
            {
                var es = from e in context.AdminPremissions where e.CategoryID == c && e.AdminID == userId select e;
                // If any pre permission was found, set flag = true
                if (es.Count() > 0)
                    es.First().AllowAccess = true;

                // Otherwise add new one
                else
                    context.AdminPremissions.AddObject(new AdminPremission() { AdminID = userId, CategoryID = c, AllowAccess = true });
            }
        }

这是一个 web 应用程序,当用户标记权限时,我只能确定设置了哪些权限,而不是所有权限。

如果您有任何其他想法或更好的方法,请告诉我。

我认为表 Permission 和 Admin 之间的关系已从实际数据库或实体 Model 中删除。 就是这种情况,然后你必须再次建立这种关系。

生成“管理员”表和“权限”删除并创建脚本(带有数据 - 如果您有数据)。

然后执行它,如果你有关系重新创建它,你的prb必须解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM