3

I'm using EF 4.3 with Ninject. I have a simple DataContext and I've created an interface for it. It's a simple interface:

public interface IMyDataContext
{
    DbSet<ComplexType> ComplexTypes { get; set; }

    int SaveChanges();
}

Implementation of MyDataContext:

    public class MyDataContext : DbContext, IMyDataContext
{
    public DbSet<ComplexType> ComplexTypes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); //Not sure if this is necessary.. 

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

And I'm adding it to NInject:

Kernel.Bind<IMyDataContext>().To<MyDataContext>();

Using the connection string name MyDataContext doesn't work and if I use IMyDataContext, it generates another database named the full assembly name. Only one table/class is mapped.. not sure if that has anything to do with it.

  <connectionStrings>
    <add name="MyDataContext"
     connectionString="Server=MyPc\SQLEXPRESS;Database=MyDataContext;Persist Security Info=True;Integrated Security=true;MultipleActiveResultSets=True"
     providerName="System.Data.SqlClient" />

I know I can override the constructor on the data context.... but I thought this was supposed to "auto map". Any thoughts?

Thanks in advance!

1 Answer 1

3

Just do a

Kernel.Bind<IMyDataContext()
    .To<MyDataContext()
    .WithConstructorArgument("nameOrConnectionString", "MyDataContext");

In conjunction with this:

public class MyDataContext : IMyDbContext, DbContext
{
    public MyDataContext (string nameOrConnectionString) 
       : base(nameOrConnectionString) { }
}

To avoid any problem.

Also what is mapped depends on your DbSet<> declaration on your DbContext, you should also post the MyDataContext implementation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - added the implementation above. I was hoping for a way to just use the name of the datacontext - but I think this is juts as easy. Appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.