I am getting data by OracleDataReader from table. And I have a model for this table.
Employee Model
public class Employee
{
public int ID { get; set; }
public int SIRKETID { get; set; }
public string NAME{ get; set; }
public string SURNAME{ get; set; }
public string DEPARTMENT{ get; set; }
public string STATUS{ get; set; }
}
Controller
public JsonResult getEmployee()
{
....
string sql = @"select * from Employee";
OracleCommand command = new OracleCommand(sql, connection);
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var employee = new Employee();
Employee.ID = Convert.ToInt32(reader["ID"]);
Employee.SIRKETID = Convert.ToInt32(reader["SIRKETID "]);
Employee.NAME = reader["NAME"].ToString();
Employee.SURNAME = reader["SURNAME"].ToString();
Employee.DEPARTMENT = reader["DEPARTMENT"].ToString();
Employee.STATUS = reader["STATUS"].ToString();
model.Add(Employee);
}
....
return Json(new { data = model});
}
This works fine, but I dont want to map columns one by one. I have 100 columns. And I dont know how much columns in some tables
Is there any simple way to return model as json
You can use third party library like Dapper, that is a simple object mapper for .Net.
You can check a basic sample of usage below:
public class Dog
{
public string Name { get; set; }
public float? Weight { get; set; }
}
var dog = connection.Query<Dog>("SOME QUERY", new { Name = "DOGNAME" });