Dapper-Extensions icon indicating copy to clipboard operation
Dapper-Extensions copied to clipboard

Insert the entity with not null ID.

Open alexpantyukhin opened this issue 2 years ago • 3 comments

Hello! Thank you for the great lib! I have an issue with Insert function. I have an Entity with 2 not null primary keys:

Entity
{
    OtherEntityId (PK, not null)
    OtherEntityField (PK, not null)
    ...
}

So there is the composite key for OtherEntityId and OtherEntityField.

When I try to put it into the DB:

conn.Insert(new Entity{OtherEntityId = 1, OtherEntityField = 'OtherField'})

I got an error: Cannot insert the value NULL into column 'OtherEntityId', table 'DB.dbo.TicketField'; column does not allow nulls. INSERT fails.

I think that it's because of these lines of code: https://github.com/tmsmith/Dapper-Extensions/blob/master/DapperExtensions/Sql/SqlGenerator.cs#L228 . It skips the primary keys values. It would be good if the lib checks if fields of the DB set up not null value and allow to put values into the insert command.

alexpantyukhin avatar Sep 07 '22 06:09 alexpantyukhin

First, I found that the primary key cannot be inserted. The primary key does not support insertion (it must rely on mysql to automatically process the value. If it is not set, an exception will be thrown if the default value is not set).

Second, there seems to be some problems with data types. MySQL defines the column as int, but C # prompts that I cannot convert the exception of type int64 to int32.

I hope I can get your fix, much appreciated.

ghost avatar Jan 09 '23 10:01 ghost

@kelilife For example: the primary key could be a composite key (2 or more fields), which consists from foreign keys on other tables.

alexpantyukhin avatar Jan 13 '23 06:01 alexpantyukhin

@alexpantyukhin I am now using another ORM framework and basically have no problems. It seems that the primary key is affected to the detriment of inserting data into it, even if it is the primary key for only one field. As for the rest I don't know much about it.

ghost avatar Jan 13 '23 15:01 ghost