2

I'm passing a DBSet as a parameter into a method, and also passing the DbContext so that I can call SaveChanges().

Is there any way to call SaveChanges() or an equivalent on the DbSet object itself?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64
  • What is it what you want to accomplish exactly.. some more words to enlighten that would be great! – Bart Teunissen May 17 '13 at 11:32
  • 1
    Why the downvotes? This is a QA site and I simply asked if such a thing were possible. Admittedly there wasn't much research, but the question is clear and useful (to someone with my level of understanding at least) – Martin Hansen Lennox May 17 '13 at 12:58
  • 1
    @Bart I just wanted to know if such a thing were possible in order to formulate an approach. I don't (yet) have a specific problem that it relates to. – Martin Hansen Lennox May 17 '13 at 13:06
  • You can access DbContext using reflection as describe in [Can you get the DbContext from a DbSet?](http://stackoverflow.com/a/17712575), but it's not recommended way – Michael Freidgeim Jul 13 '16 at 03:10
  • Thanks Michael, I didn't persue this in the end, but good to know there's an option – Martin Hansen Lennox Jul 14 '16 at 08:37

1 Answers1

4

This is not possible without creating your own abstraction, such as a Repository. An instance of DbSet does not have a reference to the parent DbContext.

qujck
  • 14,388
  • 4
  • 45
  • 74