DapperLib / DapperLib/Dapper.Contrib
IsWriteable not working on Key columns
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 293
- Forks
- 109
- PR merge metrics
- No merged PRs in 30d
Description
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:
https://github.com/DapperLib/Dapper.Contrib/blob/cf24f6bdc577b1e071c3764ddfb2cf3382531405/src/Dapper.Contrib/SqlMapperExtensions.cs#L438
(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...):
- Key column detection sucks, but thats because Dapper isn't checking EF attributes and can be fixed with ExplicitKey attribute.
- 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.
- 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?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/Dapper.Contrib/SqlMapperExtensions.cs at the linked line around 438 and reproduce the single-column table case using the C# model shown. Trace how key fields and IsWriteable fields are selected for updates, then establish expected behavior for an empty SET clause and key-column writes. Done means the behavior is defined and covered for this reported case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100