Skip to content

Bug: The primary field is not found' exception when primary key column name is different from Primary attribute property name is different while Connection.Update is called. #356

@eMTeTeam

Description

@eMTeTeam

I have a below class which represents my MySql Database table with Primary key column as 'PEOPLEID'

 [Map("[PEOPLE]")]
    public class CreatePeopleRecordModel
    {
        [Map("PEOPLEID")]
        [Primary]
        public Guid PeopleId { get; set; }

        [Map("FIRSTNAME")]
        public string FirstName { get; set; }

        [Map("LASTNAME")]
        public string LastName { get; set; }
}

When I call below code,

connection.Update(tableName, model);

"The primary field is not found."

I debugged the code and found that in below class getting the exception -
RepoDb.Core\RepoDb\Operations\DbConnection\Update.cs


// Variables needed
            var primary = DbFieldCache.Get(connection, tableName, transaction)?.FirstOrDefault(dbField => dbField.IsPrimary);
            var where = (QueryGroup)null;

            // Identity the property via primary
            if (primary != null)
            {
                var property = entity?.GetType().GetProperty(primary.Name, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
                if (property != null)
                {
                    where = new QueryGroup(new QueryField(new Field(property.Name, property.PropertyType), property.GetValue(entity)));
                }
                else
                {
                    throw new PrimaryFieldNotFoundException("The primary field is not found.");
                }
            }

The Primary key variable is resolved only based on database table column which is 'PEOPLEID', but in my class I have 'PeopleId'. It is not checking for [Primary] attribute exist in the class.

Suggest me how to solve this issue?

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdeployedFeature or bug is deployed at the current releasehelp wantedThe community is asking a helpreviewThe changes are under or being reviewed

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions