2015-10-03 30 views
0

我目前工作的一個虛擬MVC項目(嘗試一些新的東西),但我有我的注入到DatabaseContext我的服務問題......Ninject「定製」的DbContext結合

你能找到我下面的代碼:

我DatabaseContext:

public class DatabaseContext : DbContext 
{ 
    protected DatabaseContext() : base("DatabaseContext") 
    { 
    } 

    public DbSet<MacAddress> MacAddresses { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    } 
} 

我的服務&界面,我想在其中注入我的上下文:

public interface IMacAddressService 
{ 
    List<MacAddress> GetAllMacAddresses(); 
} 

public class MacAddressService : IMacAddressService 
{ 
    private readonly DatabaseContext _context; 

    public MacAddressService(DatabaseContext context) 
    { 
     this._context = context; 
    } 

    public List<MacAddress> GetAllMacAddresses() 
    { 
     return _context.MacAddresses.ToList(); 
    } 
} 

我可以在我的IKernel上應用什麼綁定來正確注入我的DatabaseContext?

爲了您的信息:

  • 我已經能夠綁定類,因此沒有什麼毛病我Ninject的設置,我只需要知道如何綁定這個特定背景下

  • 我GOOGLE了,但所有我能找到的是如何將DbContext綁定到自己...

  • 我使用EF教程中的自定義DbContext,以便我可以在我的服務中使用我的DbSets(我稍後將使用此存儲庫)

在此先感謝!

回答

1

您不使用任何抽象將DatabaseContext傳遞給您的服務,因此Ninject將在沒有任何額外配置的情況下解析它。

如果要配置顯式綁定,您可以使用Bind<DatabaseContext>().ToSelf()

編輯

我只注意到你的DatabaseContext構造函數是保護

protected DatabaseContext() : base("DatabaseContext") 
{ 
} 

你需要讓公衆能夠創建實例DatabaseContext

+0

謝謝,我在建立'DatabaseContext'的構造函數之後解決了它:) –