Kros-sk / Kros.Libs

This repo contains Kros.Utils, Kros.Utils.MsAccess, Kros.KORM and Kros.KORM.MsAccess libraries.
MIT License
7 stars 13 forks source link

Invalid primary key detection in ConventionModelMapper #156

Closed satano closed 6 years ago

satano commented 6 years ago

Library name and version

Description

We try to find primary key of the table In ConventionModelMapper.OnMapPrimaryKey() method. It looks for Key attribute and also uses a name convention. The problem is, it uses both ways at the same time, so it can wrongly find a primary key composed of two columns. One column with the attribute and the other with conventional name. It makes troubles later - for example BulkUpdate fails with the message, that primary key of the table is composite.

Steps To Reproduce

  1. Make a model class
    public class Data
    {
    [Key]
    public int RowId { get; set; }
    public int Id { get; set; }
    public string Data { get; set; }
    }
  2. Make an DbSet of this type, and add some data to it.
  3. Call DbSet.BulkUpdate().
  4. It fails with the message, that there is no primary key or it is composite. In this case, it is composite, composed of RowId column found by attribute and Id column found by convention.

Expected behavior

ConventionModelMapper should find correct primary key. The Key attribute has precedence over conventional name. So convention with Id column name should be used only if there is no column with Key attribute - in case of example, the RowId column is primary key. Consequently BulkUpdate (and other operations we do not know about yet) should work.

Notes

Maybe we should also add check that there can be only one Key attribute. We do not support composite primary keys in operations and this attribute is to ready for it either. If we want to support composite primary key to be declared with the attribute, it should have a argument which specifies the order of columns in primary key. Or maybe implement the order.