I have an MVC3 app which was using EF CTP5. After upgrading to EF 4.1 I get NullReferenceException
thrown from here:
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
I've got EF 4.1 bits from NuGet.
Database is initialized using custom initializer
public class RecreateDatabaseInitializer : IDatabaseInitializer<DatabaseContext>
{
public void InitializeDatabase(DatabaseContext context)
{
if (ConfigurationManager.ConnectionStrings["DatabaseContextSA"] == null)
{
EventLog.WriteEntry("RecreateDatabaseInitializer", "Connection string 'DatabaseContextSA' doesn't exist in config file.", EventLogEntryType.Warning);
return;
}
using (var ctx = new DatabaseContext("DatabaseContextSA"))
{
if (ctx.Database.Exists())
DropDatabase(ctx);
CreateDatabase(ctx);
InitializeDatabaseObjects(ctx);
ctx.SaveChanges();
}
PopulateDatabase(context);
context.SaveChanges();
}
}
The Exception is thrown from PopulateDatabase()
method.
Any ideas?
UPDATE:
It seems that the problem stems from the instantiation of the second DatabaseContext to manually recreate the database. It must somehow interfere with the original context.