开发者

Generic Repository Problem EF4 Code First

I've got a slight problem when using a generic repository with EF4. First let me show you the code I use to access a collection of objects (this is code in the generic reposiroy):

 public IEnumerable<T> FindAll<T>() where T : class
    {
        return ObjectContext.CreateObjectSet<T>();
    }

Below is an example of this code being invoked for a repository of type Book

_returnedBooks = _dataContext.FindAll<Book>()
            .Where(b => b.Title == _editedtitle && b.Description == _editedDescription && b.ImageUrl == _editedImageUrl);

The code above shows me querying the data context for a book that has the properties that match values I used to edited a book previously (hence the _edited prefix). But once I'd edited this book, I did not call Save Changes on the data context.

And there is the problem, even though I haven't saved changes, the FindAll (in this case book) returns the book with edited values. However, when I inspect the DbSet (required to register the class for use with Code First), 开发者_高级运维it is not in that collection.

So, I've not managed to track down any documentation for this behaviour, or whether I myself and missing the obvious.

Help meeeeeeeeeeeeeeeeeeeeeeeee :)


Are you using the same DataContext? The DataContext caches the uncommitted changes and returns them on subsequent queries, regardless of if you've called SaveChanges() or not. If you wanted to avoid retrieving the uncommitted edits, you'd need to new a new or alternate context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜