Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert the entity with not null ID. #304

Open
alexpantyukhin opened this issue Sep 7, 2022 · 3 comments
Open

Insert the entity with not null ID. #304

alexpantyukhin opened this issue Sep 7, 2022 · 3 comments

Comments

@alexpantyukhin
Copy link

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.

@ghost
Copy link

ghost commented Jan 9, 2023

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.

@alexpantyukhin
Copy link
Author

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

@ghost
Copy link

ghost commented Jan 13, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant