C#Dapper Model不仅仅更新一个元素

时间:2016-03-22 15:31:32

标签: c# asp.net-mvc dapper

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

我有以下设置

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

如果我做了一个&#34; SELECT * FROM Stages_vw&#34;在SSMS中,结果是正确的

我偶然发现了这些帖子,但我感到有些困惑,任何帮助都会非常感激

每个@chipples答案

这是由于视图中的列名不匹配

dapper-simple-mapping

dapper-intermediate-mapping

Dapper Documentation

1 个答案:

答案 0 :(得分:1)

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