dualbios / FileBaseContext

FileBaseContext is a provider of Entity Framework 7&8 to store database information in files.
MIT License
28 stars 9 forks source link

Issue with table having composite PK #4

Closed mguinness closed 1 year ago

mguinness commented 1 year ago

I think I discovered a potential issue with the provider. When you have a table which has a composite primary key the delete method will throw a DbUpdateConcurrencyException exception since TryGetValue will return null.

https://github.com/dualbios/FileBaseContext/blob/122b87754bc1d3928331d3ac2a77d0afa5c7a747/FileBaseContext/Storage/FileBaseContextTable.cs#L109-L134

I'm not sure what the best solution would be, but it is suggested in Dictionary<string[], object[]> c# TryGetValue always return null to use a custom IEqualityComparer although it might be difficult when using generic types.

mguinness commented 1 year ago

A clumsy way of getting it to work is to add the following code after var key = CreateKey(entry) to match the dictionary entry.

if (key.GetType().IsArray)
{
    foreach (var item in _rows.Keys)
    {
        if (StructuralComparisons.StructuralEqualityComparer.Equals(item, key))
        {
            key = item;
            break;
        }
    }
}
dualbios commented 1 year ago

Hi @mguinness, thanks for a great job! I merged your changes and published a new version of the package.