DapperLib / Dapper.Contrib

Dapper community contributions - additional extensions for Dapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IsWriteable not working on Key columns

PTwr opened this issue · comments

commented

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?