Diver Dan
Diver Dan

Reputation: 9963

Strange error when trying to create a database using EF4.1 Code First

Im using the EF4.1 with code first to create a database.

I have my model in mvc3 that looks like this

    public class BusinessContactDetailsModel
    {
        public int Id { get; set; }

        [Display(Name = "Contact Date")]
        public DateTime ContactDate { get; set; }

        [Range(1, 5)]
        [Display(Name = "Outcome Rating")]
        public int OutcomeRating { get; set; }

        [DataType(DataType.MultilineText)]
        public string Comment { get; set; }


        public int BusinessId { get; set; }

        [Display(Name = "Method of Contact")]
        public int FormOfContactId { get; set; }

        [Display(Name = "Follow up Date")]
        public DateTime FollowUpDate { get; set; }

    }

Which fails when I try to run the application during the build process. I know its the FollowUpProperty that is causing the problem as its a new property I have just added and whats strange is if I change the property from a DateTime to an Int it works ok.

Has anyone else had this kind of problem?

My stack trace looks like

[SqlCeException (0x80004005): An overflow occurred while converting to datetime.] System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) +125 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) +631 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) +509 System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() +46 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior) +152 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior behavior) +36 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues) +8118684 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +267

[UpdateException: An error occurred while updating the entries. See the inner exception for details.] System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +389 System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) +163 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +609 System.Data.Entity.Internal.InternalContext.SaveChanges() +326

[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.] System.Data.Entity.Internal.InternalContext.SaveChanges() +372 System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +48 System.Data.Entity.DbContext.SaveChanges() +47 System.Data.Entity.DropCreateDatabaseIfModelChanges1.InitializeDatabase(TContext context) +502 System.Data.Entity.<>c__DisplayClass21.b_0(DbContext c) +143 System.Data.Entity.Internal.<>c_DisplayClass5.b__3() +59 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +101

[DataException: An exception occurred while initializing the database. See the InnerException for details.] System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +157 System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +260 System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c) +31 System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) +147 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 action) +276 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +112 System.Data.Entity.Internal.InternalContext.Initialize() +41 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +34 System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +148 System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator() +33 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +91 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable1 source) +58 MyApplication.CRM.Controllers.BusinessController.Index() in C:\Users\sp\documents\visual studio 2010\Projects\MyApplication.CRM\MyApplication.CRM\Controllers\BusinessController.cs:23 lambda_method(Closure , ControllerBase , Object[] ) +96 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +208 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27 System.Web.Mvc.<>c_DisplayClass15.b_12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) +191 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore() +116 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 System.Web.Mvc.Async.<>c__DisplayClass81.b__7(IAsyncResult ) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.<>c_DisplayClasse.b_d() +50 System.Web.Mvc.SecurityUtil.b_0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Upvotes: 2

Views: 3629

Answers (2)

DATEx2
DATEx2

Reputation: 3502

In my case a TimeSpan was initialized to 30 hours, which caused an exception when casted to t-sql! Dumb EF 5.0 RTM!

Upvotes: 0

Diver Dan
Diver Dan

Reputation: 9963

It turns out it was because the DateTime was not nullable. I changed it to allow null dates and it works fine now.

Upvotes: 5

Related Questions