1

I have a data model which I've created using Entity Framework Code First POCO objects.

In one of my classes, I'd like to add a method 'GetUsefulInfo'.

In order to implement this method, I need to run queries against other entities in the data model.

The question is, how should I get hold of the data context from within this method? Do I need to pass it in as a parameter to the method? It feels like there should be a way to implement this without having to ask the consumers of this class do this?

Thanks, - Chris

4

1 回答 1

1

这看起来很适合UserRepository.

通常,存储库使用某种依赖注入来保存对您的上下文的引用,或者只是对您的 DataContext 的私有引用。

在那里,您可以查询任何存储库以收集所需的信息。

示例代码:

public class UserRepository : IUserRepository
{
    private readonly EntityFrameworkDataContext database =
                                                     new  EntityFrameworkDataContext();

    public User GetUserFullInfo()
    {
        try
        {
            // Your DataContext queries to return the User and all his info...
        }
        catch
        {
            // Whaterver...
        }
    }

}
于 2012-05-08T01:04:33.123 回答