I have an MVC3 application that uses EF CTP5. After upgrading to EF 4.1, I get NullReferenceExceptionfrom 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 have an EF 4.1 bit from NuGet.
The database is initialized using a 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();
}
}
An exception is thrown from a method PopulateDatabase().
Any ideas?
UPDATE:
It seems that the problem is with creating a second DatabaseContext for manually re-creating the database. This should somehow interfere with the original context.
source
share