MVC3 + Ninject + Entity framework 4

3
我有一个依赖解析器。
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<object> GetServices(Type serviceType)
        {
            return resolutionRoot.GetAll(serviceType);
        }
    }

在 global.asax.cs 文件中
// 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<DbContext>()
                    .To<SqlDataContext>();

            // IRepository to SqlRepository
            kernel.Bind<IRepository>()
                    .To<SqlRepository>();

            // IUsersServices to UsersServices
            kernel.Bind<IUsersServices>()
                    .To<UsersServices>();

            // IMessagesServices to MessagesServices
            kernel.Bind<IMessagesServices>()
                    .To<MessagesServices>();

            // IJobAdvertsServices to JobAdvertsServices
            kernel.Bind<IJobAdvertsServices>()
                    .To<JobAdvertsServices>();

            #endregion

            // Tell ASP.NET MVC 3 to use Ninject DI Container
            DependencyResolver.SetResolver(new NinjectDependencyResolvercs(kernel));
        }
        // --------------------------------------------------------------------------------- |

和类

public class SqlDataContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Profile> Profiles { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<JobAdvert> JobAdverts { get; set; }
        public DbSet<Message> Messages { get; set; }

        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasMany(x => x.Roles).WithMany(x => x.Users).Map(x =>
            {
                x.MapLeftKey(y => y.UserId, "UserId");
                x.MapRightKey(y => y.RoleId, "RoleId");
                x.ToTable("UsersInRoles");
            });

            base.OnModelCreating(modelBuilder);
        }
    }

所有依赖项都没问题,但是使用 DbContext 到 SqlDataContext 时会出现问题。如果使用以下代码:

public class SqlRepository
{
    private DbContext dataContext;
    public SqlRepository(DbContext dataContext) {
        this.dataContext = dataContext;
    }

    public DbSet<User> Users {
        get {
            return dataContext.Users;
        }
    }
}

然后。
dataContext.Users

所有其他属性都会提示此错误:

'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?)   

有没有人知道为什么 DI(依赖注入)不适用于 DbContext 类?

1个回答

4
如果我理解正确,您正在注入没有那些方法/属性的DbContext,因为它们在派生类型SqlDataContext中声明。 您需要注入SqlDataContext。如果要使用接口,则需要从SqlDataContext中提取一个接口。
编辑: Ninject在运行时绑定,而您收到的错误(我想)是在编译时。您可以通过使用动态关键字来解决此问题,但这只是绕过问题。
public class SqlRepository
{
    private dynamic dataContext;
    public SqlRepository(DbContext dataContext) {
        this.dataContext = dataContext;
    }
    ...
}

您需要做的是更改签名以使用您的SqlDataContext:
public class SqlRepository
{
 private SqlDataContextdata Context;
    public SqlRepository(SqlDataContextdata Context) {
        this.dataContext = dataContext;
    }
  ...
}

因为DbContext并不包含这些方法,只有你的SqlContext包含。而且你的SqlContext是在运行时绑定到DbContext上的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接