samedi 16 mai 2015

EF: How to access "OriginalValue" by reflection

I am using Entity Framework 6.X and I have a specific situation where I need to access the "OriginalValue" and set a new value.

I have tried some things like:

public virtual void EditWithoutGet(T original, object parameters)
    {
        var rowVersionProperty = original.GetType().GetProperty("RowVersion");
        if (rowVersionProperty != null)
        {
            var rv = rowVersionProperty.GetValue(original);

            // COMPILE but of course that here I am not applying the changes on "original". 
            // FAILS on runtime.
            _entities.Entry(typeof(T))
                .Property("RowVersion")
                .OriginalValue = rv;

            // does NOT COMPILE
            _entities.Entry(original).Property("RowVersion").OriginalValue = rv;
        }
        _entities.SaveChangesAsync().Wait();
    }

However, this is not working.... at this point I am thinking of using reflection but in this specific case I am not being able to do it. Perhaps you guys could give me a help in here using reflection or perhaps someother way that I am not thinking.

Thanks.





Aucun commentaire:

Enregistrer un commentaire