Entity Framework removes too many entries - C#

c# entity-framework entity-framework-6 sql

Question

Entity Framework is driving me crazy. It ought to be quicker and simpler, but it's awful.

I'm utilising the database-first methodology with only two very basic tables. TheUser a desk withId and Username autoincrement on, main keyId also thePermission a desk withpermissionname and userid both columns' principal keys, as well as a relationship touserid to user.id .

I use the following syntax to give and cancel permissions:

   public bool ContributionMarginCustomer
    {
        get => GetPermission(_contributionmarginCustomer);
        set => SetPermission(value, _contributionmarginCustomer);
    }

The ContributionMarginCustomer A checkbox is connected to a property.

private void SetPermission(bool permissionIsGranted, string key)
{
    var permissionStatus = permissionIsGranted ? PermissionStatus.Granted : PermissionStatus.Revoked;

    using (var entity = new KundeninfoEntities())
    {
        var user = entity.Users.Single(x => x.Id == _user.Id);
        var existingPermission = user.Permissions.SingleOrDefault(x => x.Name == key);

        switch (permissionStatus)
        {
            case PermissionStatus.Granted:
                if (existingPermission == null)
                {
                    user.Permissions.Add(new Permission { Name = key });
                    entity.SaveChanges();
                }
                break;

            case PermissionStatus.Revoked:
                if (existingPermission != null)
                {
                    entity.Permissions.Remove(existingPermission);
                    entity.SaveChanges();
                }
                break;
        }

        _permissions = entity.Permissions
                             .Where(x => x.UserId == _user.Id)
                             .ToList();
    }
}

Permission-granting works very well.

Taking away the authorization withentity.Permission.Remove(existingPermission) removes it from each and every database user.

I don't know why this behaviour is happening. Any of you do?

Thanks

EDIT: Usertable

enter image description here

permissible prior to:

enter image description here

Permissionable after my removal of it using userId 15 and the keyCONTRIBUTIONMARGINCUSTOMER

enter image description here

Edit 2: ANSWER

I modified the permissiontable such that the main key was simply one column. Accordingly, the new permissiontable contains three columns: Id (autoincrement), Name, and UserId, each of which has a different key for name and userid.

The aforementioned code now works, but I'm not quite satisfied.

1
0
8/8/2018 8:18:43 AM

Popular Answer

In the second instance, you are rescinding your authorization from the entity rather than the user (which is basically your entire list of users). Adjust to

case PermissionStatus.Revoked:
                if (existingPermission != null)
                {
                    user.Permissions.Remove(existingPermission);
                    entity.SaveChanges();
                }
                break;
2
8/8/2018 6:31:59 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow