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

  • Home
  • SEARCH
  • 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 6726603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:56:47+00:00 2026-05-26T09:56:47+00:00

I am building a reservation system. I have users in roles(‘admin’, ‘client’, ’employee’, ‘student’).

  • 0

I am building a reservation system. I have users in roles(‘admin’, ‘client’, ’employee’, ‘student’).

Each reservation must be associated with a user of role client, it might be assigned to user of role employee and might also be assigned to user of role student.

So in my reservation class I have properties of type User and I have marked them with [ForeignKey(“AnytypeId”)] attribute to hint EF for relations.

I have seen code like this at http://blog.stevensanderson.com/2011/01/28/mvcscaffolding-one-to-many-relationships/

public class Reservation
{

    public int ReservationID
    {
        get;
        set;
    }

    [Required(ErrorMessage="Please provide a valid date")]
    public DateTime ReservationDate
    {
        get;
        set;
    }
    public DateTime ReservationEnd { get; set; }
    public DateTime EntryDate
    {
        get;
        set;
    }
    public DateTime UpdatedOn
    {
        get;
        set;
    }
    public decimal Ammount
    {
        get;
        set;
    }

    public decimal? Discount { get; set; }

    [DataType(DataType.MultilineText)]
    public string ServiceDetails { get; set; }

    [DataType(DataType.MultilineText)]        
    public string Remarks { get; set; }

    public string VoucherNumber { get; set; }

    public int ServiceID
    {
        get;
        set;
    }
    public Service Service
    {
        get;
        set;
    }

    public string EmployeeId { get; set; }
    [ForeignKey("EmployeeId")]
    public User Employee { get; set; }


    public string ClientId { get; set; }
    [ForeignKey("ClientId")]
    public User Client { get; set; }


    public string StudentId { get; set; }
    [ForeignKey("StudentId")]
    public User Student { get; set; }

}
public class User
{

    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    //public Guid UserId { get; set; }

    [Key]
    [Required(ErrorMessage = "User Name is required")]
    [Display(Name = "User Name")]
    [MaxLength(100)]        
    public string UserName { get; set; }

    [Required]
    [MaxLength(64)]
    public byte[] PasswordHash { get; set; }

    [Required]
    [MaxLength(128)]
    public byte[] PasswordSalt { get; set; }

    [Required(ErrorMessage = "Email is required")]
    [DataType(DataType.EmailAddress)]
    [MaxLength(200)]
    public string Email { get; set; }

    [MaxLength(200)]
    public string Comment { get; set; }

    [Display(Name = "Approved?")]
    public bool IsApproved { get; set; }

    [Display(Name = "Crate Date")]
    public DateTime DateCreated { get; set; }

    [Display(Name = "Last Login Date")]
    public DateTime? DateLastLogin { get; set; }

    [Display(Name = "Last Activity Date")]
    public DateTime? DateLastActivity { get; set; }

    [Display(Name = "Last Password Change Date")]
    public DateTime DateLastPasswordChange { get; set; }

    public string address { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Phone { get; set; }

    public bool? IsActive { get; set; }

    public int? ClientTypeID { get; set; }
    public virtual ClientType ClientType { get; set; }

    public virtual ICollection<Role> Roles { get; set; }

    public DateTime? PackageValidity { get; set; }


    public virtual ICollection<Reservation> Reservations { get; set; }


}

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        this.HasMany(u => u.Roles)
         .WithMany(r => r.Users)
         .Map(m =>
         {
             m.ToTable("RoleMemberships");
             m.MapLeftKey("Username");
             m.MapRightKey("RoleName");
         });
    }
}

Now as I run my mvc3 EF code first app database created for me on the fly with following ERD and edmx model.
enter image description here

Now few problems that I am having:
1. When I am listing all the users of role clients their reservation property is showing always 0 even if their are reservations available in database.
2. If I am trying to delete a user of role client who have reservation in database I get following error.

The DELETE statement conflicted with the REFERENCE constraint “Reservation_Client”. The conflict occurred in database “CRSDB”, table “dbo.Reservations”, column ‘ClientId’.
The statement has been terminated.

I checked the realtions in ERD and edmx model their is no cascade delete applied to them. How can I instruct EF to delete all the reservations when deleting user of role client but not for users of role employee or student.

  • 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-05-26T09:56:48+00:00Added an answer on May 26, 2026 at 9:56 am

    This code does the trick

    public class Reservation
    {
    
        public int ReservationID
        {
            get;
            set;
        }
    
        [Required(ErrorMessage="Please provide a valid date")]
        public DateTime ReservationDate
        {
            get;
            set;
        }
        public DateTime ReservationEnd { get; set; }
        public DateTime EntryDate
        {
            get;
            set;
        }
        public DateTime UpdatedOn
        {
            get;
            set;
        }
        public decimal Ammount
        {
            get;
            set;
        }
    
        public decimal? Discount { get; set; }
    
        [DataType(DataType.MultilineText)]
        public string ServiceDetails { get; set; }
    
        [DataType(DataType.MultilineText)]        
        public string Remarks { get; set; }
    
        public String  PaymentMethod { get; set; }
        public string VoucherNumber { get; set; }
    
        public int ServiceID
        {
            get;
            set;
        }
        public virtual Service Service
        {
            get;
            set;
        }
    
    
        public string EmployeeID { get; set; }
        [ForeignKey("EmployeeID")]
        public virtual User Employee { get; set; }
    
    
        public string ClientID { get; set; }
        [ForeignKey("ClientID")]
        public virtual User Client { get; set; }
    
    
        public string StudentID { get; set; }
        [ForeignKey("StudentID")]
        public virtual User Student { get; set; }
    
    
    }
    
    public class ReservationMap : EntityTypeConfiguration<Reservation>
    {
        public ReservationMap()
        {
            this.HasOptional(r => r.Client).WithMany().WillCascadeOnDelete(true);
            this.HasOptional(r => r.Employee).WithMany().WillCascadeOnDelete(false);
            this.HasOptional(r=>r.Student).WithMany().WillCascadeOnDelete(false);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a real-time reservation system and would like to allow users to
I am building an online hotel booking system.... Using php and mysql.... Users can
Building a rails B2B application that will have various users. I'm pretty clear on
Building a GUI system and I have a few classes for different GUI components
We are building an Reservation booking system. There are three modules Customers Rooms Bookings
Building a client-side swing application what should be notified on a bus (application-wide message
Building a new Mobile Web Platform for Mobile Users to purchase & download content
Building a website that has English & Japanese speaking users, with the Japanese users
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a commercial product may use various open source libraries that have use of

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.