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

  • Home
  • SEARCH
  • 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 6346871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:06:07+00:00 2026-05-24T21:06:07+00:00

I have an entity A that used as navigation property for another entity B.

  • 0

I have an entity A that used as navigation property for another entity B. I would like to control the Insertion for entity A. Thats meen that whenever I would insert A I have to perform other checks and update other entities.
But when I Insert entities of type B it automatically inserts the connected A entities without the extra checks and updates I need.

How can I solve this?

UPDATE
I decided to use this answer as suggested. But in the OnBeforeInsert() I might add a new entities to the context which their OnBeforeInsert() won’t be called since in the time var changedEntities = ChangeTracker.Entries(); was called the new entitied wasn’t exist yet.
How can I solve this?

  • 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-24T21:06:08+00:00Added an answer on May 24, 2026 at 9:06 pm

    EF has very few extension points. So it is sometimes very difficult to customize.

    This answer is an extension of my previous answer

    public abstract class Entity
    {
        public virtual void OnBeforeInsert(){}
        public virtual void OnBeforeUpdate(){}
    }
    
    public class Category : Entity
    {
    
        public string Name { get; set; }
        public string UrlName{ get; set; }
    
        public override void OnBeforeInsert()
        {
           //ur logic
        }
    }
    

    Then in your DbContext class subscribe to ObjectStateManagerChanged event of ObjectStateManager.

    public class MyContext : DbContext
    {
        public override int SaveChanges()
        {
            //intercept entity changes
            UnderlyingObjectContext.ObjectStateManager.ObjectStateManagerChanged 
               += OnObjectStateManagerChanged;
    
            var changedEntities = ChangeTracker.Entries();
    
            foreach (var changedEntity in changedEntities)
            {
                if (changedEntity.Entity is Entity)
                {
                    var entity = (Entity)changedEntity.Entity;
    
                    switch (changedEntity.State)
                    {
                        case EntityState.Added:
                            entity.OnBeforeInsert();
                            break;
    
                        case EntityState.Modified:
                            entity.OnBeforeUpdate();
                            break;
                    }
                }
            }
    
            return base.SaveChanges();
        }
    
        ObjectContext UnderlyingObjectContext
        {
             get
             {
                  return ((IObjectContextAdapter)this).ObjectContext;
             }
        }
    
        void OnObjectStateManagerChanged(object sender, CollectionChangeEventArgs e)
        {
            if (e.Action == CollectionChangeAction.Add)
            {
                 //not all added entities are new
                 if (UnderlyingObjectContext.ObjectStateManager
                   .GetObjectStateEntry(e.Element).State == EntityState.Added)
                 {
                       if (e.Element is Entity)
                       {
                           ((Entity)e.Element).OnBeforeInsert();
                       }
                 }
            }
         }
    }
    

    If you are using EF 4.0 you will need to customize this accordingly.

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

Sidebar

Related Questions

I have some entity types that I would like to lazy load. However, they
I have an entity that has a reference to another one like these: class
I have an entity class that has a property with an underlying db column
I have a Contact entity that exposes a navigation proeprty for child Phone entities.
i have a java class used as an entity that has 2 classes that
How would one structure a table for an entity that can have a one
I have a simple form that is used to update an entity in my
I have an object defined by my entity framework model that has navigation properties,
I have an NHibernate entity that looks like this: public class Offender { public
I have an entity named Tag with a navigation property (collection) named Articles .

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.