尝试使用EF4.1 Code First创建数据库时出现奇怪错误

时间:2011-06-05 01:25:07

标签: entity-framework-4.1

我首先使用带有代码的EF4.1来创建数据库。

我的模型在mvc3中看起来像这样

    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; }

    }

当我尝试在构建过程中运行应用程序时失败。我知道它的FollowUpProperty引起了问题,因为它是我刚刚添加的一个新属性,如果我将属性从DateTime更改为Int它就可以正常工作了。

还有其他人有过这种问题吗?

我的堆栈跟踪看起来像

  

[SqlCeException(0x80004005):转换为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,Dictionary 2 identifierValues, List 1 generatedValues)+8118684      System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter adapter)+267

[UpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常。]    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:更新条目时发生错误。有关详细信息,请参阅内部异常。]    System.Data.Entity.Internal.InternalContext.SaveChanges()+372    System.Data.Entity.Internal.LazyInternalContext.SaveChanges()+48    System.Data.Entity.DbContext.SaveChanges()+ 47    System.Data.Entity.DropCreateDatabaseIfModelChanges 1.InitializeDatabase(TContext context) +502 System.Data.Entity.<>c__DisplayClass2 1.b_ 0(DbContext c)+143    System.Data.Entity.Internal。&lt;&gt; c _DisplayClass5.b__3()+59    System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)+101

[DataException:初始化数据库时发生异常。有关详细信息,请参阅InnerException。    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.RetryAction 1.PerformAction(TInput input) +147 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action 1动作)+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.InternalSet 1.Initialize() +148 System.Data.Entity.Internal.Linq.InternalSet 1.GetEnumerator()+33    System.Data.Entity.Infrastructure.DbQuery 1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +91 System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable 1 source)+58    C:\ Users \ sp \ documents \ visual studio 2010 \ Projects \ MyApplication.CRM \ MyApplication.CRM \ Controllers \ BusinessController.cs中的MyApplication.CRM.Controllers.BusinessController.Index():23    lambda_method(Closure,ControllerBase,Object [])+96    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object []参数)+17    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) +208 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2个参数)+27    System.Web.Mvc。&lt;&gt; c_ DisplayClass15.b _12()+55    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func 1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 1个过滤器,ActionDescriptor actionDescriptor,IDictionary 2 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__DisplayClass8 1.b__7(IAsyncResult )+12    System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62    System.Web.Mvc。&lt;&gt; c _DisplayClasse.b_ d()+50    System.Web.Mvc.SecurityUtil.b _0(动作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&amp; completedSynchronously)+184

2 个答案:

答案 0 :(得分:5)

事实证明这是因为DateTime不可为空。我将其更改为允许空日期,现在工作正常。

答案 1 :(得分:0)

在我的情况下,TimeSpan被初始化为30小时,这在转换为t-sql时导致异常!哑巴EF 5.0 RTM!