C# Dapper 模型不只更新一个元素

C# Dapper Model not updating only one element

我的模型似乎有问题没有更新,但似乎只有一方面。

我有以下设置

public class Stages
{
    public int ItemNumber {get; set;}   <-- seems to be updating correctly
    public string ItemName {get; set;}  <-- seems to be updating correctly
    public int ItemStage {get; set;}    <-- always results in 0
}

public interface IStageRepository
{
    List<Stages> GetAll();
}

public class StageRepository : IStageRepository
{
    private IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["StagesConnection"].ConnectionString);

    public List<Stages> GetAll()
    {
        return this.db.Query<Stages>("SELECT * FROM Stages_vw").ToList();
    }
}

在我的 HomeController 下我有以下内容

IStageRepository stagesRepo = new StageRepository();

public ActionResult Index()
{
    return (stagesRepo.GetAll());
}

这是视图的架构

TABLE_CATALOG   TABLE_SCHEMA    TABLE_NAME  COLUMN_NAME COLUMN_DEFAULT
WebServices     dbo             Stages_vw   ItemNumber  NULL
WebServices     dbo             Stages_vw   ItemName    NULL
WebServices     dbo             Stages_vw   Stage       NULL

如果我在 SSMS 中执行 "SELECT * FROM Stages_vw" 结果是正确的

我无意中看到了这些帖子,但我感到有点困惑,如有任何帮助,我们将不胜感激

每个@chipples 的答案

这是因为视图中的列名称不匹配

dapper-simple-mapping

dapper-intermediate-mapping

Dapper Documentation

根据评论,问题是您的模型和数据库中的名称不匹配(模型中的 ItemStage,数据库中的 Stage)。 Dapper 需要名称匹配才能自动映射。您应该将模型上的 属性 重命名为 Stage,或者在 SQL ("SELECT Stage AS ItemStage...") 中使用别名。