1

I'm trying to track changes of an object\table StoneUserInput. A StoneUserInputhas a foreign key to another object\table named Cut.

When I change Cut: stoneUserInput.Cut = "A" I'm receiving an indication that Cut entity has changed.

But what I need, is an Indication that changes has been made on StoneUserInput.

To retrieve changes I'm using the following code:

 var modified = DB.ObjectStateManager.GetObjectStateEntries(EntityState.Modified)

Here modified holds the Cut entity type.

Any thoughts or any ideas please?


StoneUserInput


|Navigation Property: CutGradesSet|

MichaelLo
  • 1,289
  • 1
  • 14
  • 26
  • If you change cut, then cut is modified, unless you affect the relationship or indeed the StoneUserInput then only cut will ever be modified. "Indication that changes has been made on StoneUserInput" but it has not changed, has it? Why not just change the StoneUserInput entitystate yourself, when you change the Cut entity? using something like context.Entry(StoneUserInput).State = EntityState.Modified; but I fail to see the point in marking StoneuserInput as modified if you only want to make it be saved? – Paul Zahra Feb 04 '15 at 16:03
  • Well, StoneUserInput has changed since one of it's properties : "Cut" has changed. If the property isn't a foreign key(like cut), the ObjectStateManager returns StoneUserInput. Regarding changing the entity: I don't want to go over all the methods that changes properties of StoneUserInput, I want to make all changes in a single place. – MichaelLo Feb 04 '15 at 16:08
  • If you want to detach the FK related Cut and attach another then that's a slightly different matter, I feel you should be more specific... anyhow this may interest you http://stackoverflow.com/questions/5716733/updating-foreign-key-associations-in-entity-framework-4-1-code-first – Paul Zahra Feb 04 '15 at 16:08
  • 1
    But isn't Cut an FK related entity, it isn't a property of StoneUserInput eh? – Paul Zahra Feb 04 '15 at 16:09
  • P.S. You don't have to 'go over all the methods', you could override SaveChanges in your Entity Model something like public override int SaveChanges(SaveOptions saveOptions) and do some magic in there. – Paul Zahra Feb 04 '15 at 16:14
  • As @PaulZahra mentioned, Cut might be a Navigation Property, but this is really just a proxy to get reference to it. It is not actually a property of StoneUserInput. It might be beneficial to include your Models. – Matt Sanders Feb 04 '15 at 18:40
  • I will post the model when i get to work. Cut is a navigation property, not a simple property of stoneuserinput. – MichaelLo Feb 04 '15 at 21:29
  • I rather not making any changes in the save changes area since this case is one of many entities which i want to track and it will be hard to modify their savechanges method – MichaelLo Feb 04 '15 at 21:32
  • @PaulZahra "I fail to see the point in marking StoneuserInput as modified" : That is the required business logic: when a specific StoneUserInput changes it's cut value, I need to track it. – MichaelLo Feb 05 '15 at 07:12
  • Then why not override SaveChanges and before you actually save detect if it's a cut value being changed and then do as you like. – Paul Zahra Feb 05 '15 at 08:58

0 Answers0