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

Prevent database generated columns from being inserted. #339

Closed
grounzero opened this issue Jan 20, 2020 · 5 comments
Closed

Prevent database generated columns from being inserted. #339

grounzero opened this issue Jan 20, 2020 · 5 comments

Comments

@grounzero
Copy link

grounzero commented Jan 20, 2020

[IgnoreAttribute] is great at preventing database generated columns from being added to the insert list but it prevents the column from populated during a Get and irt appears [DatabaseGenerated(DatabaseGeneratedOption.Identity)] is not recognised.

How can I work around this?

@hussamelvani
Copy link

I was looking for a solution like this as well. +1

@abravodev
Copy link

abravodev commented Nov 24, 2020

Not sure whether this can help you, but you can use Dapper.Contrib, which has an Insert method and you can use attributes ([Key], [Computed]...) that will prevent properties from being added to the insert query (it also works for update queries).

I've realized that the query builder lacks some of the features I need, but with the QueryFactory you can get the IDbConnection and then use any of the Dapper methods.

@lesair
Copy link

lesair commented Mar 16, 2021

@abravodev Yes Dapper.Contrib has a Key attribute to decorate your POCOs properties as a column whose value is generated by the database, however I noticed that SqlKata too has a [Key] attribute. I thought it would work the same as Dapper's but it didn't. Do you know what is SqlKata [Key] attribute for?

@lesair
Copy link

lesair commented Mar 16, 2021

@grounzero and @abravodev, I decorated my POCO with the [Ignore] SqlKata key attribute and it worked for insertions, SqlKata no longer includes the decorated column in the INSERT statement, but it includes it in SELECT statements, so it works for me.

@ahmad-moussawi
Copy link
Contributor

The [Key] and the [Ignore] attributes prevent the columns from being inserted/updated in the insert/update query, and they don't do anything more than that.

The select query is not affected at all.

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

5 participants