2

I´m working with Entity Framework (code first) in a MVVM application. Some "Documents" have an Status like Draft, Confirmed and so on, when a document is confirmed I want to be sure not changes are made, think about Invoices, once is confirmed/issued, user can´t change the content.

I´ve read about read-only in Entity Framework, but Views, Database level controlled, or AsNoTracking doesn´t fit well for this scenario (I think)

Some ideas?

RURIA
  • 59
  • 4

2 Answers2

0

I've never done this, but you could take an idea I saw in this answer.

Basically you might be able to override your DbContext's SaveChanges() method (or maybe DetectChanges()) to see if the original state of an entity contains some sort of flag that marks it as read only (like a FINAL bool column that when true means it's locked down). When the save or detect happens, you can make the entity look like it's not modified and it won't get saved.

This is just an idea, I have no idea if this is really possible.

Community
  • 1
  • 1
Steve
  • 6,334
  • 4
  • 39
  • 67
0

Implement IValidatableObject in your entity and throw a validation exception when the status is confirmed and the entity state is modified.

Although I would recommend using an update trigger in the database (preferred method) that did the same so the logic is carried through in any application that uses that database.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • It was little tricky get the EntityEntry from IValidatableObject.Validate method, finally it works (thanks to Diego, https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1351751-validationcontext-should-expose-owner-dbcontext) – RURIA Aug 15 '13 at 20:08