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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:46:23+00:00 2026-05-26T06:46:23+00:00

Hello I am coding in MVC 3, using C#. I am populating an Associating

  • 0

Hello I am coding in MVC 3, using C#.

I am populating an Associating table between a tutor and a student, called Tutor_Student. the Primary Key of this table consists of the tutorId and studentId, from the parent tables, and Subject.

what i have in my Model is as follows:

        [Key, Column(Order = 0)]
        public string tutorID { get; set; }
        [Key, Column(Order = 1)]
        public string studentID { get; set; }
        [Key, Column(Order = 2)]
        public string Subject { get; set; }

When the user populates this table from the website I want to ensure that the composite primary key does not already exist in the database to avoid primary key violations.

Is there any way of doing this?
Thank you in advance, i really appreciate it…

  • 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-26T06:46:23+00:00Added an answer on May 26, 2026 at 6:46 am

    One way to handle this is to have a unique constraint on the columns and let SQL Server handle the validation. Then you can just catch the SqlException thrown when saving and handle it gracefully. The SqlException class contains lots of useful information that you can dig into to figure out what went wrong, like the Errors collection.

    Another way to deal with this is to create a data access layer that contains helper methods for these sorts of things. In the repository pattern, a data Repository is a natural place to have the following method:

    bool Exists(string tutorID, string studentID, string subject)
    

    The exact implementation of this method depends on your database server and such, but by following the repository pattern, you get the required level of separation in your application to deal with these types of problems in a structured and intuitive way.

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

Sidebar

Related Questions

I have this kinda template text : Hello {#Name#}, Thanks for coming blah on
Hello can anybody solve this please I'm creating the object in the action class
Hello , I am currently working on the student association's website. The student's association
Hello I am working with a simulator that uses rcS scripts to boot, this
Hello and thanks for looking! Background I am designing a greenfield application using .NET4
Possible Duplicate: Useful Regular Expression Tutorial Hello, I recently started coding in javascript. I
Hello guys Im currently itearing over a list and im hard-coding fieldsets based on
Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the
Hello i´m coding in C# and have a problem. I have two Forms (Form1
Hello I have this code with a compiler error (error is from Microsoft Visual

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.