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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:03:49+00:00 2026-05-13T01:03:49+00:00

I have a table, say STUDENTS, that is related one-to-many to another table, CLASSES.

  • 0

I have a table, say STUDENTS, that is related one-to-many to another table, CLASSES. I created an ASP.Net form to take input of a new student, including selection of one of the classes from a combo box. This is a small but multi-tier app, and I have started using the entity framework. So the EF generated classes for both those tables. On Submit, I am building a STUDENT object and handing it off to my Data controller for inserts, updates, etc.

In the STUDENTS table I have a CLASSID that’s a FK to the CLASSES table. Back in the day, when I would do the insert to STUDENTS I would take the SelectedValue from the combo box and stick that into the ClassId field. However, with the EF, the STUDENT object doesn’t have this ID field. Instead it has a CLASS field, which is a full-on instance of that class. When the new STUDENT object is built, that CLASS field is null. I want to populate it with the class the user chose in the combo.

I can’t leave it null, and I can’t just put the ID in there. I tried doing a retrieve of that class record and using that, but I get an error that I can’t use the same object with two different contexts. What am I really supposed to be doing here?

  • 1 1 Answer
  • 2 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-13T01:03:49+00:00Added an answer on May 13, 2026 at 1:03 am

    You can set EntityKey property of ClassReference:

    Student.ClassReference.EntityKey = new EntityKey("myModel.ClassSet", "ID", classId);
    

    EDIT

    This is how to add student (I can’t check it now, but it should work):

    newStudent.BClassReference.EntityKey = new EntityKey("FormsEntities.BClasses", "BClassId", 13);
    
    FormsEntities fe = new FormsEntities();
    fe.AddToStudents(newStudent);
    fe.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a Student and a School table. One operation that I
Let's say I have a table that represents a super class, students . And
Lets say I have two databases: one for students and one for classes. I
I have a table let's say in the form of: match(id, hometeam_id, awayteam_id) and
I have a feed table with say fields: id - unique feed id created
Let's say I have this: CREATE TABLE `classes` ( `class_ID` INT AUTO_INCREMENT, FOREIGN KEY
I have a webpage. I show records from table, lets say, students in my
Say I have a table for students attending school like this: St_ID |St_Name|Class_ID|Year ------+-------+--------+----
We have a table(say T1) that is referenced by about 16 other tables with
Let's say I have a Students table on my main page, with Country, Name,

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.