简体   繁体   中英

C# MVVM ASP.net API implicit conversion error

New to web app API's and trying to build a web app that exposes data from our DWH environment to the application via an API.

My models:

bed_data_dbconnect - context model:

     using Microsoft.EntityFrameworkCore;

     namespace BED_MGMT_WEBAPP_APIs.Models
       {
         public class bed_data_dbconnect : DbContext
         {
         public bed_data_dbconnect(DbContextOptions options) : base(options)
         { }

         public DbSet<Bed_Data> tblBase_BEDMGMT_BEDS_BY_WARD { get; set; }
     }
 }

bed_data model

 using System;

 namespace BED_MGMT_WEBAPP_APIs.Models
 {
     public class Bed_Data
     {
    public string HOSPITAL {get;set;}
    public string WARD_CODE {get;set;}
    public string WARD {get;set;}
    public string REALTIME_BED_STOCK {get;set;}
    public string ACTUAL_BED_STOCK {get;set;}
    public string AVAILABLE_BEDS_M {get;set;}
    public string AVAILABLE_BEDS_F {get;set;}
    public string AVAILABLE_BEDS_C {get;set;}
    public string TOTAL_BEDS_CLOSED {get;set;}
    public string CLOSED_AND_UNOCCUPIED {get;set;}
    public string ESCALATION_BEDS_IN_USE {get;set;}
    public string DISCHARGES_CONFIRMED_M {get;set;}
    public string DISCHARGES_CONFIRMED_F {get;set;}
    public string DISCHARGES_CONFIRMED_C {get;set;}
    public string DISCHARGES_POTENTIAL_M {get;set;}
    public string DISCHARGES_POTENTIAL_F {get;set;}
    public string DISCHARGES_POTENTIAL_C {get;set;}
    public string AWAITING_ADMISSION_AE_M {get;set;}
    public string AWAITING_ADMISSION_AE_F {get;set;}
    public string AWAITING_ADMISSION_AE_C {get;set;}
    public string AWAITING_ADMISSION_INTERNAL_M {get;set;}
    public string AWAITING_ADMISSION_INTERNAL_F {get;set;}
    public string AWAITING_ADMISSION_INTERNAL_C {get;set;}
    public string AWAITING_ADMISSION_EXTERNAL_M {get;set;}
    public string AWAITING_ADMISSION_EXTERNAL_F {get;set;}
    public string AWAITING_ADMISSION_EXTERNAL_C {get;set;}
    public string AWAITING_REPATRIATION_IN {get;set;}
    public string AWAITING_REPATRIATION_OUT {get;set;}
    public string OUTLIERS_MED {get;set;}
    public string OUTLIERS_SURG {get;set;}
    public string OUTLIERS_ORTH {get;set;}
    public string STAFFING_SHORTAGES {get;set;}
    public string USER_CODE {get;set;}
    public string DATETIME_RECORD_INSERTED {get;set;} //= System.Convert.ToDateTime() 

    }
 }

db connection string in my startup file:

  public void ConfigureServices(IServiceCollection services)
    {
        string connectionString = Configuration.GetConnectionString("DefaultConnection");
        services.AddDbContext<bed_data_dbconnect>(opt => opt.UseSqlServer(connectionString));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
    }

Controller:

 using Microsoft.AspNetCore.Mvc;
 using System.Collections.Generic;
 using System.Linq;
 using BED_MGMT_WEBAPP_APIs.Models;


 namespace BED_MGMT_WEBAPP_APIs.Controllers
 {
   [Route("api/[Controller]")]
   [ApiController]

    public class bed_data_controller : Controller
    {
    private bed_data_dbconnect _bed_data;

    public bed_data_controller(bed_data_dbconnect context)
    {

    _bed_data = context;

    }

    //Get api/values
    [HttpGet]

    public ActionResult<IEnumerable<bed_data_dbconnect>> Get()
    {

        return _bed_data.tblBase_BEDMGMT_BEDS_BY_WARD.ToList();


    }

    ~bed_data_controller()
    {

        _bed_data.Dispose();

    }



     }
 }

I am trying to return the contents of this table to a list to validate the api is working as expected, but the controller above generates an error:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'Microsoft.AspNetCore.Mvc.ActionResult>' [BED_MGMT_WEBAPP_APIs]

on this line:

 return _bed_data.tblBase_BEDMGMT_BEDS_BY_WARD.ToList();

I expect the returntype of your endpoint is wrong. The returntype of your endpoint is ActionResult<IEnumerable<bed_data_dbconnect>> .

Where bed_data_dbconnect is a DbContext class.

Shouldn't the returntype be ActionResult<IEnumerable<Bed_Data>> ?

You can try it like this:

[HttpGet]
public ActionResult Get()
{
    var result=_bed_data.tblBase_BEDMGMT_BEDS_BY_WARD.ToList();
    return Json(result);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM