ObjectSet.Context vs DbSet

后端 未结 1 332
名媛妹妹
名媛妹妹 2021-01-13 03:06

i used to use the ObjectSet in EF 4.0, I could get the underlying Context for this ObjectSet using

myObjectSet.Cont

相关标签:
1条回答
  • 2021-01-13 03:58

    EF 4.1 does not offer a public API to get the DbContext from the DbSet instance. You would have to use Reflections API to get the DbContext instance.

    Edit

    One workaround would be to pass around the DbContext instace with DbSet instance. Eg if you had a constructor that took only a DbSet instance. Pass the DbContext instace also.

    public MyService(DbContext context, DbSet<MyClass> mySet){}
    

    Other method would be to open up the EntityFramewrk.dll inside Reflector and find out how to get the DbContext instace by accessing the internal/private fields of DbSet. I wouldn't advice you to use this because implementations can change.

    0 讨论(0)
提交回复
热议问题