[Solved] ASP.Net EF Core Error: Invalid column name

trisetia302
 
on Dec 09, 2022 02:39 AM
1398 Views

Hi

I want to show data from join 4 tables but until now I have not found a solution. I have made sure that the fields and columns are the same as the model that represents in the database table. When the code is debugged no error occurs, but after that the browser running the following error occurs.

Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Tamuid_tamu'.

Invalid column name 'Kamarid_kamar'.

Invalid column name 'Tipekamarid_tipe_kamar'.

Invalid column name 'Kamarid_kamar'.

Invalid column name 'Tamuid_tamu'.

Invalid column name 'Tipekamarid_tipe_kamar'.

TblReservasi.cs

public partial class TblReservasi
{
    [Key]
    public int id_reservasi { get; set; }
 
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]
    public DateTime tgl_cek_in { get; set; }
 
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]
    public DateTime tgl_cek_out { get; set; }
 
    public int lama_menginap { get; set; }
 
    public string Status { get; set; } = null!;
 
    [ForeignKey("id_tamu")]
    public int id_tamu { get; set; }
 
    [ForeignKey("id_tipe_kamar")]
    public int id_tipe_kamar { get; set; }
 
    [ForeignKey("id_kamar")]
    public int id_kamar { get; set; }
 
    //Navigation Properties
    public TblKamar Kamar { get; set; }
 
    //Navigation Properties
    public TblTamu Tamu { get; set; }
}

TblTamu.cs

public partial class TblTamu
{
    [Key]
    public int id_tamu { get; set; }
 
    [Display(Name = "Nama Tamu")]
    public string nama_tamu { get; set; } = null!;
 
    [Display(Name = "Alamat")]
    public string Alamat { get; set; } = null!;
 
    [Display(Name = "Nomer Hand Phone")]
    public string no_telpon { get; set; } = null!;
 
    [Display(Name = "Jenis Kelamin")]
    public string Jk { get; set; } = null!;
 
    [Display(Name = "Pekerjaan")]
    public string Pekerjaan { get; set; } = null!;
}

TblKamar.cs

public partial class TblKamar
{
    [Key]
    public int id_kamar { get; set; }
 
    [Display(Name="ID Tipe Kamar")]
    public int id_tipe_kamar { get; set; }
 
    [Display(Name = "Deskripsi")]
    public string Deskripsi { get; set; } = null!;
 
    [Display(Name = "Status")]
    public string Status { get; set; } = null!;
 
 
    //Navigation Properties
    public TblTipeKamar Tipekamar { get; set; }
}

TblTipeKamar.cs

public partial class TblTipeKamar
{
    [Key]
    [Display(Name ="ID Tipe Kamar")]
    public int id_tipe_kamar { get; set; }
 
    [Display(Name = "Nama Kamar")]
    public string Nama { get; set; } = null!;
 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:Rp 0,00}")]
    public int Harga { get; set; }
}

ReservasiController.cs

public async Task<IActionResult> Index()
{
    var query = await _context.TblReservasis.Where(TR=>TR.Status=="Y").Include(T => T.Tamu).Include(K => K.Kamar).ThenInclude(TK => TK.Tipekamar).ToListAsync();
    return View(query);
}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
trisetia302
 
on Dec 15, 2022 09:15 PM

The problem has been solved after following the steps at the web url address.

https://web.archive.org/web/20210224204440/https://jeremiahflaga.github.io/2020/02/16/entity-framework-6-error-invalid-column-name-_id/

Thank you so much to everyone who tried to help me solve my problem.