EF5 Code First Unit Testing Issue

Nov 1, 2013 at 3:18 PM
Edited Nov 1, 2013 at 3:44 PM

I am using EF5 code first in my project. In order to test my data access layer using Effort, I followed the tutorial to create a fake DbContext instance. The context is created and I am injecting this fake context instance to my data accessor class.

In my unit test, the first thing I do is to add some data but as soon as I call SaveChanges(), I get the following error:

System.NullReferenceException : Object reference not set to an instance of an object.
at Effort.Internal.DbManagement.DbExtensions.GetTable(Database database, String name)
at Effort.Internal.CommandActions.DbCommandActionHelper.GetTable(DbModificationCommandTree commandTree, DbContainer container)
at Effort.Internal.CommandActions.InsertCommandAction.ExecuteNonQuery(ActionContext context)
at Effort.Provider.EffortEntityCommand.ExecuteNonQuery()
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()

        public void SetUp()
            DbConnection conn = DbConnectionFactory.CreateTransient();
            _context = new CmciContext(conn);
            _dataAccessor = new CmciHydrantFlowDataAccessor(_context);            

        public void TearDown()

        public void TestGetFlowTestDataByRiskId_MatchFound()
            const string riskId = "37PA99123456";

                new NeededFireFlowArea
                        NffAreaIdentifier = 1,
                        NffAreaName = "Test"

                new NeededFireFlow
                        RiskId = riskId,
                        FlowTestDate = "12/1/2012",
                        StaticLocationHydrantFlowPsi = "Main St & Cross St",
                        NffAreaIdentifier = 1,
                        NffLocationIdentifier = 1


            IEnumerable<NeededFireFlow> neededFireFlows =  _dataAccessor.GetFlowTestDataByRiskId(riskId);
I have confirmed that my EF configuration is correct and that I can insert data using the actual context class.

Please help.

Nov 2, 2013 at 4:27 PM
Edited Nov 2, 2013 at 4:28 PM

I should add more diagnostics information to this exception. I added this to my TODO list. What happens if you add the following line in the SetUp?
Marked as answer by manu79 on 11/5/2013 at 8:26 AM
Nov 5, 2013 at 3:26 PM
Thanks tamasflamich.
Adding this line in set up makes it work. Not sure why this line is not needed in the tutorial code but is required in my code.
Aug 21, 2014 at 12:04 PM
It seems that this code does not work:
if (!ctx.Database.Exists())
Any ideas?
Sep 8, 2014 at 6:50 PM
I will check.
Sep 8, 2014 at 6:51 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.