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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:45:18+00:00 2026-05-18T02:45:18+00:00

I have a generic method in my repository which updates a property common to

  • 0

I have a generic method in my repository which updates a property common to all the objects in my edmx model:

    private void SetUpdateParams(TEntity entity)
    {
        PropertyInfo prop = typeof(TEntity).GetProperty("CommonProperty");

        prop.SetValue(entity, "Some Value", null);
    }

This property is called by the add, update and delete methods. Example:

    public void Delete(TEntity entity)
    {
        SetUpdateParams(entity);
        _objectSet.DeleteObject(entity);
        txDB.SaveChanges();
    }

This all works fabulously well, until I try to include children in a cascade delete scenario. Since the sprocs I have to use require that this certain property is set, I must now recurse through the relationships and set this property on any loaded children in the ObjectSet. Problem is I can’t seem to figure out any way to do that. Has anyone done something like this before?

  • 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-18T02:45:19+00:00Added an answer on May 18, 2026 at 2:45 am

    It’s not the easiest solution and can be a tad tedious, but I implemented what you’re needing in a project of mine by spanning the object graph, updating the property as I found it, and keeping track of what I’ve visited. It gave great flashbacks to undergrad computer science classes. =)

    Essentially, take your object, get its properties, and push them on a stack. For each property on the stack, test if it’s the property you’re looking for. Handle if match, ignore if simple data type, add to stack if complex object.

    A couple of things that aided me in my implementation:

    • Create an interface containing the property you’re looking for and make all classes that have the property implement it. It makes it easier to interrogate your classes with reflection.
    • Try to be smart with the types of complex properties you throw on the stack to be traversed. I used an interface that designates that this complex type may have a property that needs to be updated.
    • Use something (I used a HashSet) to mark objects you’ve seen and help prevent circular reference traversals.
    • I have this functionality as part of a BaseContext’s (inheriting EF’s ObjectContext) custom Save() method. I call this fixup and then call base.SaveChanges().

    Like I said, not an easy, straightforward question, but my code handles deep object graphs and updates multiple property instances. If interested, I can follow-up with some pseudo code.


    EDIT/UPDATE

    As the code shows, I’m interested in updating current UserName and DateTime values for any number of objects that may appear in the graph.

    Notes:

    • Interface IInsertedInfo contains the properties that we need to update. So if an object implements IInsertedInfo, we know to update its properties.
    • An object that implements IRequiresCurrentUserDateTime is one that has, somewhere in its graph, an object that implements IInsertedInfo.
    • This is done in context of a save with EF. I’m interrogating the ObjectStateManager for objects that need to be saved/updated.

    I don’t claim this to be the most succinct solution, but it works fine for me. Additionally, checks for IRequiresCurrentUserDateTime and other filters (some omitted for clarity’s sake) are used solely to keep the search space manageable and to avoid spanning .NET objects, non-class types, etc.

    private void HandleInsertedUserNames(string userName)
    {
        // grab any entity that requires a current user value
        var requiresUser = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified)
                                                  .Where(ose => ose.Entity is IRequiresCurrentUserDateTime
                                                                || ose.Entity is IInsertedInfo)
                                                  .Select(ose => ose.Entity);
    
        var now = DateTime.Now;
        object current;
        var seen = new HashSet<object>();
        var stack = new Stack<object>();
        // for each entity requiring a current user value...
        foreach (var obj in requiresUser)
        {
            // traverse its object graph and update any objects that implement IRequiresCurrentUserDateTime
            stack.Push(obj);
            while (stack.Count > 0)
            {
                current = stack.Pop();
                if (current != null && !seen.Contains(current))
                {
                    // mark object as seen
                    seen.Add(current);
                    // if object implements IInsertedInfo, then set its property
                    if (current is IInsertedInfo)
                    {
                        (current as IInsertedInfo).UserName = userName;
                        (current as IInsertedInfo).DateTime = now;
                        // we can continue on to the next object in the stack if we've hit an IInsertedInfo
                        continue;
                    }
    
        // REMOVED FILTERING TESTS I USED TO REDUCE SEARCH SPACE (e.g.: is modified?)
    
                    if (current is IRequiresCurrentUserDateTime)
                    {
                        // push any instance, public properties and push them to the stack
                        current.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
        // HERE YOU CAN FILTER THE PROPERTY COLLECTION VIA WHERE CLAUSES (e.g.: only certain namespace, type, etc)
                               // select the actual value of the property
                               .Select(type => type.GetValue(current, null))
                               // further filter -- only values NOT already in the requiresUser
                               // list and those that implement IRequiresCurrentUserDateTime or IInsertedInfo
                               .Where(value => !requiresUser.Contains(value)
                                               && (value is IRequiresCurrentUserDateTime
                                                   || value is IInsertedInfo))
                               .ToList().ForEach(stack.push);
                    }
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am usign EF 4 with repository patren which have a generic query method
I have this method to get a generic repository out of a dictionary: public
I have forgotten the syntax for a generic method: public static void swap <T>
I have two generic save methods in a repository class: public void Save<T>(T entity)
I have this generic method in my repository: public T GetFirstOrDefault(Expression<Func<T, bool>> where, Expression<Func<T,
I have a generic repository and would like to implement a generic GetById method.
I have a generic repository an I am trying to add a GetById method
i have written a generic repository for my base windows which have a problem
I have a bunch of Repository classes which all look a bit like the
I have the following generic method inside my class that works as a repository

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.