That was some helpful guidance. Thanks.
Lines 6-9 of the following code returns null - theBackPermission == null
So the next block, 10-14 runs. However it chokes on line 14, SaveChanges(), saying:
SQLException: Cannot insert duplicate key row in object 'dbo.sf_permissions'
with unique index 'idx_sf_permissions'.
const string snBack = SecurityConstants.Sets.Pages.SetName;
var backendNodeId = SiteInitializer.PagesNodeId;
PageNode pnBack = myPageManager.GetPageNodes().Where(p => p.Id == backendNodeId).FirstOrDefault();
if (pnBack.InheritsPermissions) myPageManager.BreakPermiossionsInheritance(pnBack);
Permission theBackPermission = pnBack.Permissions.Where(p =>
p.SetName == snBack &&
p.ObjectId == backendNodeId &&
p.PrincipalId == principalId).FirstOrDefault();
if (theBackPermission == null)
theBackPermission = myPageManager.CreatePermission(snBack, backendNodeId, principalId);
I think that Permissions.Where() (line 6) is getting something different than CreatePermissions() (line 12) is creating.
For fun, I stopped the code in debug, on line 14, and looked at all the items in pnBack.Permissions, and compared them with the theBackPermission object. Nothing in pnBack.Permissions, was the same as theBackPermission.
Please advise how I can check for the permisssion and then and the permission I need correctly.