joozek
joozek

Reputation: 2191

How to retrieve original value of collection

I have a collection mapped in my model:

public class Project
{
    // ...
    public virtual ICollection<ProjectSupplier> ProjectSuppliers {get; set;}
}

And I want to retrieve original value of ProjectSuppliers collection (I know for sure that it has been loaded). I tried:

var originalProjectSuppliers = _context.Entry(project)
    .OriginalValues
    .GetValue<ICollection<ProjectSupplier>>("ProjectSuppliers");

But it gives me error:

System.ArgumentException : The 'ProjectSuppliers' property does not exist or is not mapped for the type 'Project'

I also tried getting DbCollectionEntry like that:

_context.Entry(project).Collection(p => p.ProjectSuppliers)

But it doesn't contain OriginalValues, only current ones.

Upvotes: 1

Views: 302

Answers (1)

S&#39;pht&#39;Kr
S&#39;pht&#39;Kr

Reputation: 2839

Apparently this is the only way. Not what I would have hoped either, but the answer is written by a guy who works on Entity Framework, so I guess he would know.

UPDATE

Based on the linked answer above, I've built something perhaps more in line with your actual question, or at least you can use it to get back the original collection. I'm not sure it's the best way though, so I posted my own question on how to do it better.

Upvotes: 1

Related Questions