2015-05-28 24 views
0

實體框架是否會處理GetFooFromUnmappableEntity方法中使用的SqlConnection?實體框架在使用SqlQuery時如何處理連接<T>

我有以下服務:

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(); 

回答

0

Ninject是「依賴倒置」的實現,這意味着你的依賴是接口和Ninject會給你的實現。您需要創建一個IFooRepository,在其實現中使用FooContext。然後IFooRepository被注入到IFooService的構造函數中,並且IFooService不需要知道如何實現存儲庫的任何信息。

至於配置的SqlConnection的,你會想你的綁定倉庫.InRequestScope()並使用OnePerRequestModulewhich will dispose your objects at the end of the request.

+0

因爲,正如我所說,出現在類庫這些綁定我不能使用InRequestScope,不是一個asp .net項目。 – Alex