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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:31:26+00:00 2026-06-16T10:31:26+00:00

i have two simple models that have one-many relation public class X{ public int

  • 0

i have two simple models that have one-many relation

public class X{
  public int id {get;set;}
  public virtual Y y{ get; set; }
  public string description{get;set;}
}
public class Y{
  public int id{get;set;}
  public string name{get;set;}
}

and from the entity framework i set the relation

I’m trying to submit a form that creates a new X record with existing Y value

the returned data from the form to the action is the string description and y_id.

on the action when i try to specify the object from the parameters

public ActionResult sth(X x){}

the x.y is set to null (which is expected…) while when i tried the following …

x.y = (from i in Y where i.id == that_id select i).first(); 
...
db.saveChanges();

the entity framework inserted a new Y record to the database …

i know that i’m doing something wrong … your help will be very appreciated !

  • 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-16T10:31:27+00:00Added an answer on June 16, 2026 at 10:31 am

    It seems an issue with tracking Y objects in the dbContext. how are you generating the collection Y you querying with linq? do you fetch it from the database using a dbContext? or from some other source?

    if not, you need to be fetching it using a context or using the Attach() function so it could be properly tracked.

    Another question pops up. how come a new Y is inserted? Don’t you have a primary key defined in the database? or is it try to insert then an exception is thrown?

    These are my thoughts on the matter.

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

Sidebar

Related Questions

I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have two dependent models with one to many association class Post < ActiveRecord::Base
Let's say I have two simple models project t.string :title vote t.references :project t.integer
I have a simple Fluent NHibernate model with two related classes: public class Applicant
I have these two simple files that define a C++ class with a tryME
I have two models, one is Group, the other Item. A group has many
So let's say I have two :has_and_belongs_to_many models that are joined by a simple
I have a simple has_one/belongs_to relationship between two models. This is a new association
I have two relatively simple codes. One main activity and one intent service. Main
I have two very simple, identical UITableViews in my app that are populated with

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.