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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:49:38+00:00 2026-06-06T14:49:38+00:00

In MVC3 Code First EF how do you associate one entity with another without

  • 0

In MVC3 Code First EF how do you associate one entity with another without creating a new one (many-to-many)?

So I have a many-to-many relationship between class1 and class2. I need class1 to hold many class2 and vice versa. However, class2 is independent; I have a list of them that I want to edit separately and then associate with a new class1.

When I pass my class2List to the controller( via AJAX and JSON), I checked and all the Ids of the class2s correspond to existing ids in the db, i.e. new class2s are not created.

Model

class
{
   [key]
   public int Id {set; get;} 
}
class1 : class 
{
        private ICollection<class2> _class2s;
        public virtual ICollection<class2> class2s
        {
            get { return _class2s ?? ( _class2s = new HashSet<class2>()); }
            set { _class2s = value; }
        }  
}

class2 : class
{
        private ICollection<class1> _class1s;
        public virtual ICollection<class1> class1s
        {
            get { return _class1s ?? ( _class1s = new HashSet<class1>()); }
            set { _class1s = value; }
        }  
}

Controller

public ActionResult SaveChanges(List<class2> class2List)
    {
        createNewClass2AndAssociateExistingClass2s(class2List);
        SaveChangesToDb();
        return View("ProductDetail", Model);
    }

createNewClass2AndAssociateExistingClass2s(List<class2> class2List)
{
    var newClass1 = newClass1()
    {
      class2s = class2List;
    }

    ////UnitOfWork allows me to access several DbSets in one transaction
    unitOfWork.Create(newClass1) 

}
SaveChangesToDb()
{
    unitOfWork.Commit();
}

What this does is create a new class1 (as it should) but instead of associating the existing class2s with it, it makes new class2s with new Ids and adds them to the database.

My question:

Does this have to do with how EF is reading my Id property from base class?

How would I be able to associate several existing class2s as a list with a new class1, without creating new class2s in the database?

Cheers

  • 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-06T14:49:40+00:00Added an answer on June 6, 2026 at 2:49 pm

    Ok so two things I learned from figuring this out:

    • I was inheriting from an abstract class when I should have been implementing an interface. This is a great idea if you have several entities that have a similar property such as “Id” and you want to do something like

      T FindById<T>(int id) where T : IEntity

    • When making associations in EF, even if the Id matches an existing entry, it will not update that entry, unless EF is tracking that entry in the context, as it says here. What I needed to do was:

      • Add a method in the mapping layer that gets the entry by id that I
        want from the repository
      • Copy the attributes of the new entry into that context entry
      • Return the context entry

    Hope this helps someone

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

Sidebar

Related Questions

I have the following classes in an Entity Framework 4.3 Code-First Model for MVC3:
I have developed a project using MVC3 and Code first Entity Framework 4.0 as
I am using MVC3 with Entity Framework Code First. I have the following model.
I am creating a very simple EF4 code first project with one entity. public
MVC3 project, using LINQ to Entity, and Entity Framework 4 Code-First. In another post
I have an ASP.NET MVC3 application, where I use Entity Framework 4.3 Code-First and
I have an Entity Framework Code First MVC3 solution where I have an entity
I am using ASP.NET MVC3 with EF Code First. I have not worked previously
I just created a new project in MVC3 using EF4 code first deployed on
Using Entity Framework 4.1 code first and POCO entities in MVC3; I would like

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.