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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:56:09+00:00 2026-05-27T00:56:09+00:00

I want to add a relationship between multiple existing entities and another existing entity.

  • 0

I want to add a relationship between multiple existing entities and another existing entity. Here is my model:

public class Term
{
    public int TermId { get; set; }
    public virtual ICollection<SubForm> SubForms { get; set; }
}

public class SubForm
{
    public int SubFormId { get; set; }
    public virtual ICollection<Term> Terms { get; set; }
}

I have an update repository method as follows:

public IQueryable<Term> GetTerms()
{
    IQueryable<Term> query = db.Terms.AsNoTracking();
    return query;
}

public Term UpdateTerm(Term term, IEnumerable<Expression<Func<Term, object>>> properties)
{
    if (term.TermId == 0)
    {
        throw new InvalidOperationException("Term does not exist");
    }
    db.Terms.Attach(term);
    if (properties != null)
    {
        foreach (var selector in properties)
        {
            string propertyName = Helpers.PropertyToString(selector.Body);
            db.Entry(term).Property(propertyName).IsModified = true;
        }
    }
    db.SaveChanges();
    return term;
}

Now I assume this would work when I make this call in my service layer:

public void AddFormToTerm(int termId, int formId)
{
    var term = termsRepository.GetTerms().FirstOrDefault(t => t.TermId == termId);
    var subForms = termsRepository.GetSubForms().Where(t => t.FormId == formId);

    //I assume this would work by adding existing forms to an existing term.
    foreach (var subForm in subForms)
    {
        term.SubForms.Add(subForm);
    }

    termsRepository.UpdateTerm(term, null);
}

Unfortunately, this doesn’t get updated, there is nothing in the intermediate table when I checked the database. No exception was also thrown.

  • 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-27T00:56:10+00:00Added an answer on May 27, 2026 at 12:56 am

    Using AsNoTracking in this case is the problem. Without AsNoTracking it will work. You must keep in mind that you can update a many-to-many relationship only with the change tracking mechanism. But in your code the EF context will know about term and the SubForms collection for the first time when you call Attach in your UpdateTerm method. EF does not notice that you did add the SubForms to the term because those entities were not attached to the context (since you used AsNoTracking = “EF, please do not attach to the context!”). But after Attach nothing happened anymore before you called SaveChanges = No change = No database commands.
    So removing AsNoTracking (or creating another method or a parameter to load with tracking) is the best option. Everything else will involve ugly “tricks” like this:

    public Term UpdateTerm(Term term, ...)
    {
        //...
    
        // Restore the state before adding the subforms = current state in DB
        var tempSubForms = term.SubForms;
        term.SubForms = null;
    
        // Inform EF about this state = term exists, subforms exist
        // in DB but no relationships
        db.Terms.Attach(term);
        foreach (var subForm in tempSubForms)
            db.SubForms.Attach(subForm);
    
        // Change the state: EF change tracking recognizes this
        term.SubForms = tempSubForms;
    
        //...
    
        // EF now will send INSERT statements for the join table
        db.SaveChanges();
        return term;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many relationship between Foo and Bar Here's a simplified Foo public
I have dbml with single table users i want add partial class for User
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I want to add a column to an existing legacy database and write a
I want to add a property to my User model that returns the number
I am trying to add a relationship between 2 tables, but i am getting
I have a many-to-many relationship defined between two entities using an association table and
I am using Entity Framework 4. I have a many-to-many association (relationship) between two
Relationship between my domains : class Cartridge { ... static hasMany = [cartridgeLanes: CartridgeLane]
I have many-to-many relationship between Game and Account models like below: class Account <

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.