1

I have used EF many times, but i cant seem to figure out why this is happening.

I have a solution with 2 projects, one is my common library used in a few other applications that has all my entities and one is my updater console application. In the common library i have my context which looks like so.

public class DalContext : DbContext
{
    public DalContext() : base(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)
    {
        Database.SetInitializer(new CreateDatabaseIfNotExists<DalContext>());
    }

    public DbSet<UserAccount> UserAccounts { get; set; }
    public DbSet<GameImport> GameImports { get; set; }

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

then in my console application i go to create an instance of the context, and i get the error

"Object reference not set to an instance of an object".

I am using a basic using statement to create my context so everything gets disposed properly.

using (var db = new DalContext())
{
    ....
}

i get the error when the using statement is creating the DalContext object.

both applications are using the same version of EF 6.1.3. I was able to do an add-migration just fine. i dunno, i'm stumped.

I have referenced other applications where i have used this and i cannot find any differences as to why this would be happening. I know its going to be something blatantly obvious.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
Barqster7764
  • 57
  • 1
  • 10

1 Answers1

0

In this kind of situation, it's always a good practice to do a binary search debugging. That is, keep on removing stuff until it works. Narrow down the error. Until the bare minimum.

You can try:

  • Replace this by the actuall connection string: ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString. This is the most likely one. "DefaultConnection" may not be on your web.config/app.config file, that would explain why the same code works for other host projects.
  • Remove/replace this: Database.SetInitializer(new CreateDatabaseIfNotExists<DalContext>());
  • Remove/replace this: modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();.

Given the info you have provided, it's impossible for us to help you further.

Community
  • 1
  • 1
Andre Pena
  • 56,650
  • 48
  • 196
  • 243