C# 使用SqlQuery时实体框架如何处理连接
实体框架是否会处理GetFooFromUnmappableEntity方法中使用的SqlConnection 我有以下服务:C# 使用SqlQuery时实体框架如何处理连接,c#,entity-framework,ninject,C#,Entity Framework,Ninject,实体框架是否会处理GetFooFromUnmappableEntity方法中使用的SqlConnection 我有以下服务: public class FooService { private readonly FooContext fooContext; public FooService(FooContext fooContext) { this.fooContext = fooContext; } public Foo GetFooFro
public class FooService {
private readonly FooContext fooContext;
public FooService(FooContext fooContext) {
this.fooContext = fooContext;
}
public Foo GetFooFromUnmappableEntity(int id) {
return fooContext.Database.SqlQuery<Foo>(string.Format("select * from GetFoo({0})", id);
}
}
我正在使用Ninject管理类库中的依赖项。因此,存在这样的绑定:
Bind<FooContext>.ToSelf();
Ninject是依赖项反转的一个实现,这意味着您的依赖项是接口,Ninject将为您提供实现。您将希望创建一个在其实现中使用FooContext的IFooRepository。然后IFooRepository被注入到IFooService的构造函数中,IFooService不需要知道任何关于如何实现存储库的信息
至于处理SqlConnection,您需要绑定repository.InRequestScope并使用OnePerRequestModule我不能使用InRequestScope,因为正如我所说,这些绑定发生在类库中,而不是asp.net项目中。