Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8043849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:02:49+00:00 2026-06-05T05:02:49+00:00

I am trying to learn asp.net MVC, by converting a web forms app I

  • 0

I am trying to learn asp.net MVC, by converting a web forms app I have. It’s a room booking app, where there is a customer table (tblCustomerBooking) which has a one to many relationship with tblRental – so one customer can book more than one room. The fields that match each other are tblCustomerBooking.customer_id -> tblRental.customer_ref

I’m trying to use code first – and building a model class – but I can’t figure out how to link the two tables, so that when I query the dbContext, it will return a customer, with one or more rentals within the same model.

My table definitions are:

CREATE TABLE [dbo].[tblCustomerBooking](
    [customer_id] [bigint] IDENTITY(1,1) NOT NULL,
    [room_id] [bigint] NULL,
    [customer_name] [varchar](110) NULL,
    [customer_email] [varchar](50) NULL
     CONSTRAINT [PK_tblCustomerBooking] PRIMARY KEY CLUSTERED 
(
    [customer_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

CREATE TABLE [dbo].[tblRental](
    [rental_id] [bigint] IDENTITY(1,1) NOT NULL,
    [room_id] [bigint] NOT NULL,
    [check_in] [datetime] NOT NULL,
    [check_out] [datetime] NOT NULL,
    [customer_ref] [bigint] NULL,
    [room_cost] [decimal](18, 2) NULL
 CONSTRAINT [PK_tblRental_1] PRIMARY KEY CLUSTERED 
([rental_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS      =     ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

My attempt at building the model for this is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions; 
using System.Data.Entity;

namespace MvcApplication23.Models
{
    public class tblRental 
        {
        [Key()]
        public int rental_id { get; set; }
        public int room_id { get; set; } 
            public DateTime check_in { get; set; } 
            public DateTime check_out { get; set; }
            public long customer_ref { get; set; } 
            [ForeignKey("customer_ref")] 
            public tblCustomerBooking Customer {get;set;} 
            public decimal room_cost { get; set; } 
        } 

    public class tblCustomerBooking 
    {
        [Key()]
        public long customer_id { get; set; } 
        public string customer_name { get; set; } 
        public string customer_email { get; set; } 
        public ICollection<tblRental> Rentals {get;set;} 
    }

    public class RentalContext : DbContext
    {
        public DbSet<tblCustomerBooking> customers { get; set; }
        public DbSet<tblRental> rentals { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        } 
    }
}

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using MvcApplication23.Models;

namespace MvcApplication23.api.Controllers
{
    public class RentalController : ApiController
    {
        private RentalContext db = new RentalContext();

        // GET /api/rental/5
        public IQueryable<tblCustomerBooking> Get(int id)
        {
            return db.customers.Include("rentals").FirstOrDefault(c=>c.customer_id==id);
        }

** I’ve updated the info above, with the actual table names that already existed in the database **

How to I link the two tables in the model? And then given a customer_id, how would I query the DbContext to return a customer, with any related entries in the tblRental table?

Thank you very much for any pointers,

Mark

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:02:51+00:00Added an answer on June 5, 2026 at 5:02 am

    To link two entities, provide a navigation property:

    public class Rental
    {
        [Key]
        public int rental_id { get; set; }
        public int room_id { get; set; }
        public DateTime check_in { get; set; }
        public DateTime check_out { get; set; }
        public int customer_ref { get; set; }
    
        [ForeignKey("customer_ref")]
        public virtual Customer Customer {get;set;}
    
        public decimal room_cost { get; set; }
     }
    
    public class Customer
    {
        [Key]
        public int customer_id { get; set; }
        public string customer_name { get; set; }
        public string customer_email { get; set; }
    
        public virtual ICollection<Rental> Rentals {get;set;}
    }
    

    And to query your customer :

    return this.DataContext.customers.Include("Rentals").FirstOrDefaul(c=>c.customer_id==customerId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn ASP.NET MVC 3 coming from a web forms background. I'm
I'm trying to learn ASP.NET MVC, and I want to have menus highlighted on
I am trying to learn MVP It is using web forms in ASP.NET. I
I am trying to learn ASP.Net MVC framework and I was wondering if there
I am trying to learn Asp.net mvc. I know its different from forms and
I am trying to learn ASP.NET MVC and I hit this problem: I have
I am trying to learn ASP.NET MVC by porting my current app written in
I am trying to learn asp.net 4 web app but playing around the web
I am trying to learn ASP.net MVC 2 and I have created the DB
I am trying to learn asp.net MVC and having an issue to submit a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.