Entity Framework removes too many entries - C#

c# entity-framework entity-framework-6 sql

Question

I'm freaking out with Entity Framework. It should be easier and faster but it's just horrible.

I'm using database first approach with two very simple tables. The User table with Id and Username, primary key and autoincrement on Id, and the Permission table with permissionname and userid, primary key on both columns and a releation from userid to user.id.

This is my code to grant and revoke permissions:

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

The ContributionMarginCustomer property is bound to a checkbox.

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();
    }
}

Granting a permission works great.

Removing the permission with entity.Permission.Remove(existingPermission) removes it from every single user in the database.

I do not understand this behaviour. Do any of you?

Thanks

EDIT: Usertable

enter image description here

Permissiontable before:

enter image description here

Permissiontable after I remove it with userId 15 and key CONTRIBUTIONMARGINCUSTOMER

enter image description here

Edit 2: SOLUTION

I changed the permissiontable to have only one column as primary key. That means the new permissiontable has three columns: Id (autoincrement), Name and UserId with a unique key to name and userid.

The code above works now but I'm not really happy with this.

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

Popular Answer

In your second case, you are not removing your permission from the user, but from the entity (which is basically your entire list of users). Change 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