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
Make a model class
public class Data
{
[Key]
public int RowId { get; set; }
public int Id { get; set; }
public string Data { get; set; }
}
Make an DbSet of this type, and add some data to it.
Call DbSet.BulkUpdate().
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.
Library name and version
Description
We try to find primary key of the table In
ConventionModelMapper.OnMapPrimaryKey()
method. It looks forKey
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 exampleBulkUpdate
fails with the message, that primary key of the table is composite.Steps To Reproduce
DbSet
of this type, and add some data to it.DbSet.BulkUpdate()
.RowId
column found by attribute andId
column found by convention.Expected behavior
ConventionModelMapper
should find correct primary key. TheKey
attribute has precedence over conventional name. So convention withId
column name should be used only if there is no column withKey
attribute - in case of example, theRowId
column is primary key. ConsequentlyBulkUpdate
(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.