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

Cannot insert an object when the primary key is a string #603

Open
LeifUK opened this issue Feb 8, 2021 · 2 comments
Open

Cannot insert an object when the primary key is a string #603

LeifUK opened this issue Feb 8, 2021 · 2 comments

Comments

@LeifUK
Copy link

LeifUK commented Feb 8, 2021

I am using PetaPoco.NetCore.

The following does not work:

 _database.Insert("tblConfiguration", "name", configuration);

_database is an instance of PetaPoco.NetCore.Database. The table is created as follows:

_database.Execute("CREATE TABLE tblConfiguration (name VARCHAR(100) NOT NULL PRIMARY KEY, value VARCHAR(767) NOT NULL);");

I have to do this instead:

            string sql = "INSERT INTO tblConfiguration (name, value) VALUES (@0, @1);";
            _database.Execute(sql, configuration.name, configuration.value);

This works fine with the .Net version of PetaPoco.

@iadaz
Copy link
Collaborator

iadaz commented Mar 18, 2021

It sounds like you're using the fork of PetaPoco from https://github.com/qingask/PetaPoco.NetCore

The PetaPoco Nuget package that supports .net core is PetaPoco.Compiled: https://github.com/CollaboratingPlatypus/PetaPoco/wiki/V6NetStandard2-0

I'm afraid I can't tell you what breaking changes might exist between the latest PP and the fork.

@asherber
Copy link
Collaborator

@LeifUK I agree with @iadaz, but can you tell us what error you're getting?

Also, you can wrap your Insert() statement in a try..catch block in order to trap the exception, and in the catch you can examine _database.LastCommand to see what SQL was executed. This will help identify what the problem is.

If I had to guess, I'd say that your POCO class defines Name as the primary key but doesn't specify AutoIncrement=false. This makes PetaPoco think that the value for Name will be generated by the database, so it doesn't include that field in the SQL qhen you do Insert().

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

3 participants