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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:43:36+00:00 2026-06-10T10:43:36+00:00

I am working on Database first approach with Entity Framework 4.1. I have 2

  • 0

I am working on Database first approach with Entity Framework 4.1. I have 2 many to many related entities Book and Library. I also have one junction table with 10 columns to store information about the particular copy of the book in Library with one to many relation with both BOOK and LIBRARY entities. I have created a view which submits a JSON object with child object in it which matches the definition of my BookVieMOdel. The view is working just fine but I am having trouble adding new entries to the junction table in the controller. I am getting error saying “Adding a relationship with an entity which is in the Deleted state is not allowed“.
I guess the error is referring to the deleted object which were deleted when by the book.LibraryBookCopies.Clear(); but can’t think of doing it other way.

Thanks,

CONTROLLER:

[HttpPost]
    public ActionResult Edit(BookViewModel bookv)
    {

        Mapper.CreateMap< BookViewModel,Book>();

        Book  book = Mapper.Map<BookViewModel,Book>(bookv);

        db.Books.Attach(book);

        db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified);

        //Add library ref to each libraryBookCopies object in Book
        foreach (LibraryBookCopy lBC in book.LibraryBookCopies)
        {
            lBC.Library = db.Libraries.Single(l => l.LibraryId == lBC.LibraryId);
        }

        //Remove all currently related records
        book.LibraryBookCopies.Clear();

        //Add copies from ViewModel to book object
        foreach (LibraryBookCopy lBC in bookv.LibraryBookCopies)
        {      book.LibraryBookCopies.Add(lBC); //Error here      }

        db.SaveChanges();

        return RedirectToAction("Index");     
    }
  • 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-10T10:43:38+00:00Added an answer on June 10, 2026 at 10:43 am
     [HttpPost]
            public ActionResult Edit(BookViewModel bookv)
            {
                //create maps
                Mapper.CreateMap<LibraryBookCopyViewModel, LibraryBookCopy>();
                Mapper.CreateMap<BookViewModel, Book>();
    
                //convert view objects to model objects
                Book book = Mapper.Map<BookViewModel, Book>(bookv);
    
                List<LibraryBookCopy> bookCopiesFromView = Mapper.Map<List<LibraryBookCopyViewModel>, List<LibraryBookCopy>>(bookv.LibraryBookCopies);
    
                List<LibraryBookCopy> toBeDeletedLibraryBookCopies = new List<LibraryBookCopy>();
    
                //this has to be executed before db.Books.Attach
                book.LibraryBookCopies.Clear();
    
                //Add or update book copies
                foreach (LibraryBookCopy bc in bookCopiesFromView)
                {
                    if (bc.LibraryBookCopyId != 0)
                    {
                        db.LibraryBookCopies.Attach(bc);
                        db.ObjectStateManager.ChangeObjectState(bc, EntityState.Modified);
                    }
                    else
                    {
    
                        db.LibraryBookCopies.Attach(bc);
                        db.ObjectStateManager.ChangeObjectState(bc, EntityState.Added);
                    }
    
                }
    
                db.Books.Attach(book);
    
                if (bookCopiesFromView.Count == 0) //if all the copies are delted from view, delete all from 
                {
                    db.LibraryBookCopies.Where(c => c.BookId == book.BookId).ToList().ForEach(l => db.LibraryBookCopies.DeleteObject(l));
                }
                else
                {
                    toBeDeletedLibraryBookCopies = book.LibraryBookCopies.Except(bookCopiesFromView).ToList();
                    foreach (LibraryBookCopy bc in toBeDeletedLibraryBookCopies)
                    {
    
                        db.ObjectStateManager.ChangeObjectState(bc, EntityState.Deleted);
                    }
                }
    
                db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with Entity Framework (Model First approach) against a PostgreSQL 9.1 database. You
I have a question related to database design. The database that I'm working with
I have a model as shown below (created using Model-First approach). A book is
I have Book and Author entities with many to many relation and I am
I'm working with an existing database that has first name and last name seperated
I'm working on my first rails app deploy and the database data from my
I have a database working in my local sql server 2005 express edition. I
I'm working on database migration program and I'm trying to migrate one database to
Does anybody have an example of working with database using Visual C++ and OLEDB?
Assume we have an ASP.NET MVC3 Web site that makes use of Entity Framework

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.