【问题标题】:Strange error when trying to create a database using EF4.1 Code First尝试使用 EF4.1 Code First 创建数据库时出现奇怪的错误
【发布时间】:2011-06-05 01:25:07
【问题描述】:

我使用 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): 转换为日期时间时发生溢出。] System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 小时)+125 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) +631 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior 行为,String 方法,ResultSetOptions 选项)+509 System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() +46 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteReader(CommandBehavior 行为)+152 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior 行为)+36 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior 行为)+10 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator 翻译器,EntityConnection 连接,Dictionary2 identifierValues, List1 generatedValues) +8118684 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter 适配器) +267

[UpdateException:更新条目时出错。有关详细信息,请参阅内部异常。] System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter 适配器) +389 System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) +163 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions 选项)+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.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:初始化数据库时发生异常。有关详细信息,请参阅 InnerException。] System.Data.Entity.Internal.InternalContext.PerformInitializationAction(动作动作)+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 动作)+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 源) +58 C:\Users\sp\documents\visual studio 2010\Projects\MyApplication.CRM\MyApplication.CRM\Controllers\BusinessController.cs:23 中的 MyApplication.CRM.Controllers.BusinessController.Index() lambda_method(闭包,ControllerBase,对象[])+96 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+17 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +208 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 参数) +27 System.Web.Mvc.c_DisplayClass15.b_12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 过滤器,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(动作 f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(动作动作)+22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

【问题讨论】:

  • 原来是因为 DateTime 不可为空。我将其更改为允许空日期,现在可以正常工作了。

标签: entity-framework-4.1


【解决方案1】:

原来是因为 DateTime 不可为空。我将其更改为允许空日期,现在可以正常工作。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-06-10
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多