I'm trying to get all records from SQL database using DapperExtensions.
But I have a Schema set to other than dbo for some tables. Hence, the table is not recognized from sql query.
For example, a table is in the form [Schema][TableName]. But when I start query, error is thrown like:
Invalid object name 'TableName'.
This is the Model class:
using System;
using Dapper.Contrib.Extensions;
using ImOnTech.Teftis.Core.Models;
using ImOnTech.Teftis.Core.Models.DT_Inspection;
namespace ImOnTech.Teftis.Core.Models.DT_Inspection
{
[Table("DT_Inspection.City")]
public class City
{
This is the function to GetAll records from database:
public async Task<IReadOnlyList<City>> GetAllAsync()
{
var CityList = await Context.Connection.GetListAsync<City>();
Context.Connection.Close();
return CityList.ToList();
}
While mapping your models, be bit more explicit. Mention the
Schemaexplicitly.Following is an example how to provide various mapping properties.
Please note that, if your column names and property name in model is same, you do not need to call
Mapfor each property (the way I did aboveMap(x => x.CustomerID).Key(KeyType.Identity);). Instead, only callAutoMap();and properties will be automatically mapped.To make these mappings known to Dapper Extensions, call the following code only once at application startup: