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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:54:33+00:00 2026-05-29T14:54:33+00:00

I am working on a change analyzer for Entity Framework and have one small

  • 0

I am working on a change analyzer for Entity Framework and have one small problem left.

I need to get the Property (PropertyInfo) of the parent in a relation (1-1, 1-n, n-n).

Right now I can see the relation, the parent and the related entries, but I cannot seem to get the actual property of the object. The problem as I see it is that the relation knows about the keys, which are PKs and FKs and not the property that was responsible for the relation in the first place. In my solution now, I use the EntitySet name from the related entity, but that just isn’t good enough.

Here is the code that resolves the relations. I’ve left out the entity objects as well as the non relational analyzing parts. The IHaveLogEntries interface is to be implemented on Entities that want to be analyzed.

Also, this code runs in DbContext.SaveChanges()

this.ChangeTracker.DetectChanges();

var objectContext = ((IObjectContextAdapter)this).ObjectContext;

var entityEntries = objectContext.ObjectStateManager
    .GetObjectStateEntries(EntityState.Unchanged | EntityState.Modified | EntityState.Added)
    .ToList();

var relationEntries = objectContext.ObjectStateManager
    .GetObjectStateEntries(EntityState.Added | EntityState.Deleted)
    .ToList();

var q = from relationEntry in relationEntries
    from entityEntry in entityEntries
    where relationEntry.IsRelationshipForKey(entityEntry.EntityKey)
    where !(entityEntry.Entity is IHaveLogEntries)
    let otherEndKey = relationEntry.OtherEndKey(entityEntry.EntityKey)
    let logEntryEntity = entityEntries
        .Where(e => e.EntityKey == otherEndKey)
        .Select(se => se.Entity)
        .Cast<IHaveLogEntries>()
        .Single()
    let combined = new
    {
        RelationEntry = relationEntry,
        EntityEntry = entityEntry,
        LogEntryEntity = logEntryEntity
    }
    group combined by 
        new { combined.LogEntryEntity, combined.EntityEntry.EntitySet.Name }
        into combinedGroup
        let added = combinedGroup
            .Where(c => c.RelationEntry.State == EntityState.Added)
            .Select(c => c.EntityEntry.Entity.ToString())
        let deleted = combinedGroup
            .Where(c => c.RelationEntry.State == EntityState.Deleted)
            .Select(c => c.EntityEntry.Entity.ToString())
        select new
        {
            combinedGroup.Key.LogEntryEntity,
            Field = combinedGroup.Key.Name,
            Operation = Operation.Modified,
            FromValue = (object)string.Join(",", deleted.ToArray()),
            ToValue = (object)string.Join(",", added.ToArray())
        };

var l = q.ToList();

The main problem is where I group the result, where I use combined.EntityEntry.EntitySet.Name. I would like to figure out the name of the property on the IHaveLogEntries entity instead if that is possible. That entity would be the combined.LogEntryEntity in this case.

Thankful for your input.

  • 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-29T14:54:34+00:00Added an answer on May 29, 2026 at 2:54 pm

    I’ve solved it now using the ObjectStateEntry.EntitySet property of the relationEntry that I had. With that, I can see the name of the relation like this Entity_Property and I simply do a string extraction of the property name there which I then use to get the PropertyInfo object from the Entity.

    Then I’ve added am Attribute so that I can decorate properties with better names if necessary.

    This is what I added somewhere in the middle there:

    let propertyName = relationEntry
        .EntitySet
        .Name
        .Split("_".ToCharArray())
        .Last()
    let property = logEntryEntity
        .GetType()
        .GetProperty(propertyName)
    let nameAttribute = property
       .GetCustomAttributes(typeof(NameAttribute), true)
       .Cast<NameAttribute>()
       .FirstOrDefault()
    let name = nameAttribute != null ? nameAttribute.Name : property.Name
    

    I then use the name to group on instead:

    let combined = new {
       FieldName = name,
       RelationEntry = relationEntry,
       EtityEntry = entityEntry,
       LogEntryEntity = logEntryEntity
    }
    group combined by new { combined.LogEntryEntity, combined.FieldName } 
       into combinedGroup
    

    It does seem to work so far, even though I would like to know if anyone has a better idea. The string management feels a bit dodgy.

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

Sidebar

Related Questions

I have a problem with a my Web service and i need help. I
I have problem to change the border color of the visited thumbnails using Galleriffic.
In our application, we have two sets of tables: One set of working tables
how to change the working directory of fsi in VS. how to auto load
I am going to change my working sphere from Enterprise Web Applications written for
cd is the shell command to change the working directory. How do I change
in F# interactive:how to display/change current working directory?
How can I change the current working directory from within a Java program? Everything
I'm working on using Find/Replace to change a bunch of labels to DataBound text.
I decided to change a utility I'm working on to use a tabpage. When

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.