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 7839843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:34:02+00:00 2026-06-02T15:34:02+00:00

So I have the following models public class User { public int Id {

  • 0

So I have the following models

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
 }

public class Appointment
{
    public int Id { get; set; }
    public string Name{ get; set; }
}

and I want them assigned to a another table with a separate primary key like so

public class UserAppointment
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int AppointmentId { get; set; }

    public virtual Appointment Appointment {get;set;}
    public virtual User User {get;set;}
 }

What I wanted is the Id be a key (unique, auto generate, etc) while at the same time will force the UserId and AppointmentId to have a unique combination as well.

eg
this is what i want
  Id         UserId          AppointmentId
  1            1                  2
  2            1                  3

and not this
  Id         UserId          AppointmentId
  1            1                  2
  2            1                  2
  3            1                  2

At the moment I did some modelbuilder statements on my context to configure my keys

modelbuilder.Entity<UserApointment>()
  .HasRequired(e => e.User)
  .HasMany(u => u.Appointment)
  .HasForeignKey(e => e.UserId);

modelbuilder.Entity<UserApointment>()
  .HasRequired(e => e.Appointment)
  .HasMany(u => u.Users)
  .HasForeignKey(e => e.AppointmentId);

But it will still let me insert records with similar user and appointment ids.

Any advice on how to deal with this is very much appreciated!
Thanks!!

  • 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-02T15:34:04+00:00Added an answer on June 2, 2026 at 3:34 pm

    You can tackle this at the database level by simply creating a unique constraint on the UserAppointment table.

    ALTER TABLE UserAppointments
    ADD CONSTRAINT uc_UserAppointment UNIQUE (UserID,AppointmentID)
    

    On Entity Framework (which I am not familiar with) seems to be done like this:

    modelbuilder.Entity<UserApointment>().HasKey(x=> new { x.UserId,x.AppointmentId});
    

    Read more here., specially the remark:

    If the primary key is made up of multiple properties then specify an
    anonymous type including the properties. For example, in C# t => new {
    t.Id1, t.Id2 }

    UPDATE

    Probably a Unique Index serves your purposes better:

    CREATE UNIQUE INDEX IX_UserAppointment
    ON UserAppointment (UserId, AppointmentId)
    

    This unique index will ensure that no duplicate combination of UserId and AppointmentId will ever exist in the table and will also speed up your queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Models : public class EvaluationCriteriaItem { public int ID {
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
I have the following model: public class Product { public int Id { get;
I have the following models: User: public class User : IEntity, INamedType { public
Yo i have the following nhibernate class: public class User { public virtual int
i have this model on mvc: public class User { public string Name {
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have the following types and classes: namespace MVC.Models public class Page { public
So I have the following model: public class Person { public String FirstName {
I have a model as follows: public class User : System.Web.Security.MembershipUser { public int

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.