Monday, April 25, 2011

Entity Framework get CurrentContext

I'm extending an entities' partial class to have a method. How do I get a reference to the context that the entity is attached to (if any) to get more entities from the same context.

If that's not clear, basically the code I'm looking to write is along these lines (air code):

public void AssignSize(int width, int height)
    var size = (from s in this.context.Sizes
                where s.width == width && s.height == height
                select s).FirstOrDefault();


Nb: This doesn't work.

From stackoverflow
  • You need to pass the context to this method, or, better yet, rather than pass in width and height, pass in the size object itself.

    DeletedAccount : Point taken on the lameness of the example :o) So no way of doing it without passing the context in? That's a shame.
    Andrew Peters : Consider this: var myEntity = new MyEntity(); myEntity.AssignSize(1,2); This is just one problem with coupling an entity to a particular context.


Post a Comment