Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 11:11

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Entity Framework updating detached objects

Discussion on Entity Framework updating detached objects within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
Buckyx's Avatar
 
elite*gold: 0
Join Date: Jun 2009
Posts: 177
Received Thanks: 2
Entity Framework - optimizing update of detached objects

Guys I need your help

Loading parents from DB, then closing context. After certain point when those parents are processed(added toys to their children, toys can be shared amongst children: many to many relationship), they need to be updated and each one is updated only once.

Code:
    public class Parent
    {
        public int ParentId { get; set; }
        public virtual List<Child> Children { get; set; }
    }
    
    public class Child
    {
        public int ChildId { get; set; }

        public int ParentId { get; set; }
        public virtual Parent Parent { get; set; }

        public virtual List<Toy> Toys { get; set; }
    }

    public class Toy
    {
        public int ToyId { get; set; }

        public virtual List<Child> Child { get; set; }
    }
they have other properties, but only toys of each children are inserted. then parents are updated in DB.

Code:
    public void UpdateParents(List<Parent> parents)
    {
        using (var ctx = new Context())
        {
            foreach (var parent in parents)
            {
                parent.Children.ForEach(c => 
                {
                    ctx.Entry(c).State = EntityState.Modified;
                    c.Toys.ForEach(t => ctx.Entry(t).State = EntityState.Added));
                }

                ctx.Parents.Attach(parent);
                ctx.Entry(parent).State = EntityState.Modified;
            }

            ctx.SaveChanges();
        }
    }
Is there a better way to update detached entities which contains many to many relationship?

edit..fixed the initial problem, however I still need to optimize
edit2.. graphdiff solved the initial problem in a better fashion, still not optimal but I guess thats too much work for bulk update which also update relates entities to be worth
Buckyx is offline  
Reply


Similar Threads Similar Threads
[RELEASE] Alexya Framework - Lightweigh PHP Framework
07/13/2015 - Coding Releases - 2 Replies
Hi! I've just finished a framework I was working on and decided to release it. I'll put the github link over here: https://github.com/manulaiko/alexya and you'll check it, once I get back home I'll post a tutorial. See you!
which packet tells client entity has left range?
01/22/2011 - CO2 Programming - 9 Replies
I raised this question because i cannot figure out which packet actually informs the client that a particular entity has left the char's detectable range. I looked up korvac's doc, it's slightly outdated. The first few fields are most packets are still valid, but the latter fields are not quite accurate now. Anyway, i realized that the remove entity packet type of general data is sent to client when an entity is completely removed from game; Eg, a mob is killed, a client dc'ed. If a...
Ofsets to entity table for Radar
01/06/2009 - General Gaming Discussion - 1 Replies
Hello everyone, i have big problem to locate offsets for radar. Using CE 5.4 im trying to locate count of entities ( it should follow a pointer on array of entities ) - but its really hard to find well controlled place in game where i can control an amount of entities around me. Probably there are other methods to track this pointer down? Like reverse pointer search from Name of char? Or name of random entity? Thanks in advance.
Special Entity
09/01/2005 - World of Warcraft - 4 Replies
Well, I was doing a stockade gangbang to lvl my mage. I pulled all the mobs to the beginning, and waited in the wall. As I looked on the minimap and point in the "Mob Train" I saw this strange "Jcffb0b" Entity/Player/Mob/Npc/Shit. I was like goddammnitagmigottagetthefuckouttahere. When I did it again later, there were the same thing... Maybe someone knows what it is? Don't think it's something harmful anymore, just curious about it :P ...



All times are GMT +1. The time now is 11:13.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.