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

IsWriteable not working on Key columns #141

Open
PTwr opened this issue Jul 27, 2022 · 0 comments
Open

IsWriteable not working on Key columns #141

PTwr opened this issue Jul 27, 2022 · 0 comments

Comments

@PTwr
Copy link

PTwr commented Jul 27, 2022

So here I sit and switch from EF to Dapper because CodeFirst with several thousands of AddOrUpdate in Seed was getting a bit irritating when debugging thanks to our beloved Change Tracking... usual story of people switching to Dapper...

... and then I encountered this:

public class WhoInTarnationThoughtThatDozensOfTablesWithOneColumnIsGoodIdea
{
    [Key]
    [Dapper.Contrib.Extensions.ExplicitKey] //dapper fails to detect his as Key automatically :(
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [StringLength(250)]
    public string SomethingNotNamedIdToMakeMyDayWorse { get; set; }
}

So first issue was "Entity must have at least one [Key] or [ExplicitKey] property ", but there are already tickets for it and can be solved with that silly ExplicitKey attribute. No biggie. Build, start, make a tea while this overweight abomination of a project starts up and munches through hundred migrations... oh different exception.

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'where'...

Here's the culprit:

var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties)).ToList();

(You won't get exception if you have non-key fields in table. Single-column table is a must.)

So there are 3 problems (aside of database design I got stuck with...):

  1. Key column detection sucks, but thats because Dapper isn't checking EF attributes and can be fixed with ExplicitKey attribute.
  2. Invalid SQL when there is no "non-id properties" as SET clause ends up empty. Would be nice to have explicit exception here instead of raw error.
  3. Due to offending line [IsWriteable(true)] is not working on Key columns like [DatabaseGenerated(DatabaseGeneratedOption.None)] does in EF, because all Key columns are excluded from list of updateable fields.

So I'd like to ask, before wasting half an hour to fix it, IsWriteable vs ExplicitKey a feature or a bug?

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