0

I seem to be having a problem with my model not updaing, but it seems there is only one aspect that.

I have the following setup

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();
    }
}

Under my HomeController I have the following

IStageRepository stagesRepo = new StageRepository();

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

Here is the schema of the view

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

If I do a "SELECT * FROM Stages_vw" in SSMS the results are correct

I have stumbled across these posts but I am feeling a little confused any help would be greatly appreciated

ANSWER per @chipples

This was due to a column name in the view not matching

dapper-simple-mapping

dapper-intermediate-mapping

Dapper Documentation

Community
  • 1
  • 1
ondrovic
  • 1,105
  • 2
  • 23
  • 40
  • 1
    Ty explicitly naming the selected fields instead of `select *` – markpsmith Mar 22 '16 at 15:33
  • 1
    What is the schema of your table Stages_vw? – Steve Mar 22 '16 at 15:36
  • @markpsmith same result when specifying `select ItemName, ItemNumber, ItemStage` – ondrovic Mar 22 '16 at 15:49
  • @Steve I have updated the original post with schema – ondrovic Mar 22 '16 at 15:52
  • 2
    I may be missing something but I can't see your db schema - does the database column name definitely match the property name in the model? – chipples Mar 22 '16 at 15:54
  • @chipples I hadn't saved the schema yet, also that was the problem, the column was named Stage in the view once I updated to ItemStage it's now displaying the correct information thanks, figured it was something simple I missed – ondrovic Mar 22 '16 at 15:57
  • @chipples is correct - the column is displayed as Stage and the object property is called ItemStage. They must be identically named. – overslacked Mar 22 '16 at 16:00

1 Answers1

1

As per the comments, the problem is that the names on your model and in your database do no match (ItemStage in the model, Stage in the DB). Dapper requires the names to match in order to map automatically. You should either rename the property on your model to Stage, or use an alias in your SQL ("SELECT Stage AS ItemStage...").

chipples
  • 195
  • 1
  • 9