Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

IsWriteable not working on Key columns

未关闭
#141 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp, sql
领域
backend, database

调研方向

从 src/Dapper.Contrib/SqlMapperExtensions.cs 中链接行附近的第 438 行开始,使用所示的 C# 模型复现单列表情况。跟踪更新时如何选择键字段和 IsWriteable 字段,然后确定空 SET 子句和写入键列时的预期行为。当已定义并覆盖此报告案例的行为时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

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...):

  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?

主要语言
C#
星标
293
派生
108
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DapperLib/Dapper.Contrib 的其他 Issue

查看 DapperLib/Dapper.Contrib 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。