Get typed access to the DbEntityEntry<T>.OriginalValues property bag
C#
Latest commit a64e7c6 Nov 21, 2016 @NickStrupat Update README.md

README.md

EntityFramework.TypedOriginalValues

Get typed access to the OriginalValues of your entity properties. Simple and complex properties are supported, navigation/collections are not.

EF version .NET support NuGet package
6.1.3 == 4.0 || >= 4.5 NuGet Status
Core 1.1 >= 4.5.1 || >= Standard 1.3 NuGet Status

Usage

using (var context = new Context()) {
    var me = await context.People.SingleAsync(x => x.Name == "Nick");
    me.EmployeeNumber = 42; // change the value

    // but wait! maybe we want to see what the value was inside some other mechanism, after we changed it (i.e. logging, auditing, etc.)

    // old and busted
    var og = (int) context.Entry(me).Property(nameof(EmployeeNumber)).OriginalValue;

    // new hotness
    var og = context.GetOriginal(me).EmployeeNumber;

    await context.SaveChangesAsync(); // save that new value
}

How it works

A type is emitted at run-time which wraps the change tracking object of your DbContext. This type inherits from your entity type, so you get typed access to its properties. It behaves like a read-only snapshot of your entities original state.

Contributing

  1. Create an issue
  2. Let's find some point of agreement on your suggestion.
  3. Fork it!
  4. Create your feature branch: git checkout -b my-new-feature
  5. Commit your changes: git commit -am 'Add some feature'
  6. Push to the branch: git push origin my-new-feature
  7. Submit a pull request :D

History

Commit history

License

MIT License