title: MVC3 + Ninject + Entity framework 4
body: <p>i have this Dependency resolver</p>
  
  <pre><code>public class NinjectDependencyResolvercs : IDependencyResolver
      {
          private readonly IResolutionRoot resolutionRoot;
          public NinjectDependencyResolvercs(IResolutionRoot kernel)
          {
              resolutionRoot = kernel;
          }
  
          public object GetService(Type serviceType)
          {
              return resolutionRoot.TryGet(serviceType);
          }
  
          public IEnumerable&lt;object&gt; GetServices(Type serviceType)
          {
              return resolutionRoot.GetAll(serviceType);
          }
      }
  </code></pre>
  
  <p>in global.asax.cs</p>
  
  <pre><code>// Ninject DI container ----------------------------------------------------------- |
          public void SetupDependencyInjection()
          {
              // Create Ninject DI kernel
              IKernel kernel = new StandardKernel();
  
              #region Register services with Ninject DI Container
  
              // DbContext to SqlDataContext
              kernel.Bind&lt;DbContext&gt;()
                      .To&lt;SqlDataContext&gt;();
  
              // IRepository to SqlRepository
              kernel.Bind&lt;IRepository&gt;()
                      .To&lt;SqlRepository&gt;();
  
              // IUsersServices to UsersServices
              kernel.Bind&lt;IUsersServices&gt;()
                      .To&lt;UsersServices&gt;();
  
              // IMessagesServices to MessagesServices
              kernel.Bind&lt;IMessagesServices&gt;()
                      .To&lt;MessagesServices&gt;();
  
              // IJobAdvertsServices to JobAdvertsServices
              kernel.Bind&lt;IJobAdvertsServices&gt;()
                      .To&lt;JobAdvertsServices&gt;();
  
              #endregion
  
              // Tell ASP.NET MVC 3 to use Ninject DI Container
              DependencyResolver.SetResolver(new NinjectDependencyResolvercs(kernel));
          }
          // --------------------------------------------------------------------------------- |
  </code></pre>
  
  <p>and class </p>
  
  <pre><code>public class SqlDataContext : DbContext
      {
          public DbSet&lt;User&gt; Users { get; set; }
          public DbSet&lt;Profile&gt; Profiles { get; set; }
          public DbSet&lt;Role&gt; Roles { get; set; }
          public DbSet&lt;JobAdvert&gt; JobAdverts { get; set; }
          public DbSet&lt;Message&gt; Messages { get; set; }
  
          protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;User&gt;().HasMany(x =&gt; x.Roles).WithMany(x =&gt; x.Users).Map(x =&gt;
              {
                  x.MapLeftKey(y =&gt; y.UserId, "UserId");
                  x.MapRightKey(y =&gt; y.RoleId, "RoleId");
                  x.ToTable("UsersInRoles");
              });
  
              base.OnModelCreating(modelBuilder);
          }
      }
  </code></pre>
  
  <p>all dependecies work fine but for DbContext to SqlDataContext is problem. If use this:</p>
  
  <pre><code>public class SqlRepository
  {
      private DbContext dataContext;
      public SqlRepository(DbContext dataContext) {
          this.dataContext = dataContext;
      }
  
      public DbSet&lt;User&gt; Users {
          get {
              return dataContext.Users;
          }
      }
  }
  </code></pre>
  
  <p>then </p>
  
  <pre><code>dataContext.Users
  </code></pre>
  
  <p>and all others properties alert this error:</p>
  
  <pre><code>'System.Data.Entity.DbContext' does not contain a definition for 'JobAdverts' and no extension method 'JobAdverts' accepting a first argument of type 'System.Data.Entity.DbContext' could be found (are you missing a using directive or an assembly reference?)   
  </code></pre>
  
  <p>Have anyone any idea why DI doent work for Class DbContext ?</p>