[Solved] ASP.Net Core MVC Error: SqlException: Invalid object name

bistgeneral
 
on Dec 06, 2022 12:00 AM
482 Views

Hello, 

I am trying my hands on ASP.NET Core MVC. I have developed 1 view, controller and model. When I try to show the data on the homepage using VieModel, it gives me error "SqlException: Invalid object name 'PremiumEarned'."

My table names are MIS_PRM_EARNED_AS_ON_DATE and MIS_CLM_PAID_AS_ON_DATE

My model code is as below.

public class MIS_PRM_EARNED_AS_ON_DATE
{
    [Key]
    public decimal PRM_AMOUNT { get; set; }
}

public class MIS_CLM_PAID_AS_ON_DATE
{
    [Key]
    public decimal CLM_AMOUNT { get; set; }
}

My ViewModel class is as below.

public class HomePageVM
{
    public List<MIS_PRM_EARNED_AS_ON_DATE> PremEarned { get; set; }
    public List<MIS_CLM_PAID_AS_ON_DATE> ClmPaid { get; set; }
}

My Controller Code is as below.

private readonly ApplicationDbContext _db;
public HomeController(ApplicationDbContext db)
{
    _db = db;
}

public async Task<IActionResult> Index()
{
    HomePageVM VM = new HomePageVM();
    VM.PremEarned = await _db.PremiumEarned.ToListAsync();
    VM.ClmPaid = await _db.ClaimPaid.ToListAsync();
    return View(VM);
}

In the view, I am using the below code to show the data.

@Model.PremEarned

Where am I going wrong? Why is it saying table does not exist?

Thanks.

Regards

Shailesh

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 06, 2022 01:09 AM

Make sure that the selected Database contains the table PremiumEarned.

bistgeneral
 
on Dec 06, 2022 01:29 AM

Hi Dharmendr,

Is it mandatory to have the exact table name in the dbcontext class?

I changed it to the exact table name and it worked. Earlier, I had used an alias name(PremiumEarned).

Regards

dharmendr
 
on Dec 06, 2022 01:41 AM
bistgeneral says:
Is it mandatory to have the exact table name in the dbcontext class?

Yes.