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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:27:27+00:00 2026-06-09T22:27:27+00:00

Using the following code: using (ICMSEntities db = new ICMSEntities()) { productObj.Sectors.Clear(); int[] selected_sectors

  • 0

Using the following code:

using (ICMSEntities db = new ICMSEntities())  
{  
    productObj.Sectors.Clear();  
    int[] selected_sectors = cblSectors.Items.Cast<ListItem>()  
        .Where(n => n.Selected).Select(n => Convert.ToInt32(n.Value)).ToArray();  
    for (int i = 0; i < selected_sectors.Length; i++)  
    {  
        int SectorID = selected_sectors[i];  
        Sector sectorObj = db.Sectors.SingleOrDefault(x => x.sector_id == SectorID);  
        productObj.Sectors.Add(sectorObj);  
        }  
    db.SaveChanges();  
    Response.Redirect("~/Products.aspx", true);  
}

I am trying to update the many to many relationship tables. Each sector can have a set of products and each product can have a set of sectors. When trying to update a product entity, I am clearing all sectors in case the user chose other available sectors from the checkboxlist using .Clear() above. And then reading from the checkboxlist and updating. Instead of updating the records, I am getting a new identical row in products with the new auto incremented ID. So it’s doing an insert instead of an update and i never specified .AddObject().

What am i doing wrong here? Or how should I implement this correctly?

Thanks.

  • 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-09T22:27:28+00:00Added an answer on June 9, 2026 at 10:27 pm

    Instead of this:

    Sector sectorObj = db.Sectors.SingleOrDefault(x => x.sector_id == SectorID);  
    

    Do this:

    Sector sectorObj = db.Sectors.Find(SectorID);  
    

    If you don’t want your code to incur database roundtrip with .Find, use this LoadStub method instead: http://www.ienablemuch.com/2011/08/entity-frameworks-nhibernate_02.html

    public static class Helpers
    {
     
        public static Ent LoadStub<Ent>(this DbContext db, object id) where Ent : class
        {
            string primaryKeyName = typeof(Ent).Name + "Id";
            return db.LoadStub<Ent>(primaryKeyName, id);
        }
     
        public static Ent LoadStub<Ent>(this DbContext db, string primaryKeyName, object id) where Ent: class
        {
            var cachedEnt = 
                db.ChangeTracker.Entries().Where(x => ObjectContext.GetObjectType(x.Entity.GetType()) == typeof(Ent)).SingleOrDefault(x =>
                {
                    var entType = x.Entity.GetType();
                    var value = entType.InvokeMember(primaryKeyName, System.Reflection.BindingFlags.GetProperty, null, x.Entity, new object[] { });
     
                    return value.Equals(id);
                });
     
            if (cachedEnt != null)
            {
                return (Ent) cachedEnt.Entity;
            }
            else
            {
                Ent stub = (Ent) Activator.CreateInstance(typeof(Ent));
     
                 
                typeof(Ent).InvokeMember(primaryKeyName, System.Reflection.BindingFlags.SetProperty, null, stub, new object[] { id });
     
     
                db.Entry(stub).State = EntityState.Unchanged;
     
                return stub;
            }
     
        }
    }
    

    Your code shall be like this:

    Sector sectorObj = db.LoadStub<Sector>(SectorID);  
    

    Sample usage: http://www.ienablemuch.com/2011/07/using-checkbox-list-on-aspnet-mvc-with_16.html

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

Sidebar

Related Questions

I am creating date using following code try { newdatetime = new DateTime(2012, 2,
i am using following code $doc = new DOMDocument(); $doc->strictErrorChecking = false; @$doc->loadHTML($data); $xpath
I created a DB using following code. Dim conn As New SqlConnection(Server=.\SQLExpress;Data Source=;Integrated Security=SSPI)
I am using following code to setup connection string: ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder
I am using following code in as3: var Request:URLRequest = new URLRequest(); var xmlMain:XML=new
Using following code I try to get updated list of checkbuttons' corresponding text values,
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am using following code for showing a MessageBox with ok and cancel button.
I am using following code to show a spinning wheel: $(#loading) .hide() .ajaxStart(function(){ $(this).show();
I am using following code to get bitmap from url. This function is used

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.