IdentityServer / IdentityServer3.EntityFramework

EntityFramework persistence layer for IdentityServer3
Apache License 2.0
68 stars 97 forks source link

Deleting Entity from Navigation Property Collection Causes Update Exception #56

Closed lsuarez5280 closed 9 years ago

lsuarez5280 commented 9 years ago

Given the nature of the way related collections are only exposed via navigation properties, I assumed it was necessary to perform change resolution for each navigation property on write-back of a record. However, I can't delete related entities because there are no identifying relationships established with foreign keys either in the model builder or with decorators. Any attempt to call .Remove() on the navigation property produces the following exception:

A relationship from the 'Client_ClientSecrets' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Client_ClientSecrets_Target' must also in the 'Deleted' state.

The code looks similar to:

private void SyncClientSecrets(Thinktecture.IdentityServer.EntityFramework.Entities.Client entity, Client model)
{
    // Insert logic here

    var removedSecrets = entity.ClientSecrets.Where(cs => true).ToArray(); // Not actual remove test logic

    foreach (var secret in removedSecrets)
    {
        entity.ClientSecrets.Remove(secret);
    }
}

If this was not the intended method for updating related entities or I simply don't know of a better way EF offers to set the the related collection of entities, please let me know and feel free to disregard the report.

brockallen commented 9 years ago

Are you seeing this on 1.x? This was fixed on the dev/2.0.0 branch.

lsuarez5280 commented 9 years ago

That's correct. If it's already corrected in the next release, please disregard. We're currently using the 1.2 release. I wired up the same logic in the custom store implementation to work around the issue. Thanks for pointing me to the 2.0 code.

brockallen commented 9 years ago

np