【问题标题】:Object cannot be cast from DBNull to other types Entity Framework对象不能从 DBNull 转换为其他类型的实体框架
【发布时间】:2011-04-22 18:19:18
【问题描述】:

我正在更新两个相关的实体对象,我们将它们称为 Order 和 OrderDetails。除了主字段之外的每个字段在数据库中都可以为空。

下面是代码sn-p:

ClientEntities ce = new ClientEntities();
    Order order = (from p in ce.Orders
                                 where p.OrderID == OrderID                                
                                 select p).FirstOrDefault();

    decimal discountpercent = decimal.Parse(rntb_DiscountPercent.DbValue.ToString());

                        foreach (OrderDetail oDetail in order.OrderDetails)
                        {
                            decimal listprice = iDetail.PurchasedItem.ListPrice.GetValueOrDefault();
                            decimal discountvalue = listprice * (discountpercent / 100);
                            decimal paidprice = Decimal.Round(listprice - discountvalue, 2);

                            oDetail.ClientAmountReceived = paidprice;
                        }
                        order.DiscountPercent = rntb_DiscountPercent.Value;
                        ce.SaveChanges(); 

当上下文将更改提交到数据库时,我收到以下错误:

System.InvalidCastException:对象 不能从 DBNull 转换为其他 类型。

at System.DBNull.System.IConvertible.ToInt16(IFormatProvider

提供者) 在 System.Convert.ChangeType(对象 值,类型转换类型, IFormatProvider 提供程序) 在 System.Data.Mapping.Update.Internal.Propagator.Evaluator.Cast(对象 值,类型 clrPrimitiveType) 在 System.Data.Mapping.Update.Internal.Propagator.Evaluator.Visit(DbCastExpression 节点) 在 System.Data.Common.CommandTrees.DbCastExpression.Accept[TResultType](DbExpressionVisitor1 visitor) at System.Data.Mapping.Update.Internal.Propagator.Evaluator.Evaluate(DbExpression node, PropagatorResult row, Propagator parent) at System.Data.Mapping.Update.Internal.Propagator.Project(DbProjectExpression node, PropagatorResult row, TypeUsage resultType) at System.Data.Mapping.Update.Internal.Propagator.Visit(DbProjectExpression node) at System.Data.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor1 游客) 在 System.Data.Mapping.Update.Internal.Propagator.Propagate(UpdateTranslator 父级,EntitySet 表, DbQueryCommandTree umView) 在 System.Data.Mapping.Update.Internal.UpdateTranslator.d_0.MoveNext() 在 System.Linq.Enumerable.d_711.MoveNext() at System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable1 命令,UpdateTranslator 翻译器) 在 System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands() 在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter 适配器) 在 System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager 实体缓存) 在 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions 选项) 在 System.Data.Objects.ObjectContext.SaveChanges() 在 BSpace.InvoicePage.lbtn_CalculateDiscount_Click(对象 o, EventArgs e) 在 C:\项目\客户端\主干\客户端 Application\Source\ClientProject\OrderPage.aspx.cs:line 1069 在 System.EventHandler.Invoke(对象 发件人,EventArgs e) 在 System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 事件参数) 在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 源控件,字符串事件参数) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 发布数据) 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint)

在数据库中,Orders.DiscountPercent 为 (float, null),OrderDetails.ClientAmountReceived 为 (money, null)。

不知道为什么会发生这种情况,因为这是我要更新的仅有的两个字段。我已尝试确保每个字段中都有数据,但仍然出现此错误。我还检查了数据模型文件,以确保所有关联字段的表映射都是正确的。 OrderDetails.ClientAmountReceived 转换为 Nullable,Order.DiscountPercent 转换为 Nullable。

我一辈子都想不通为什么会这样。

【问题讨论】:

    标签: c# asp.net linq-to-sql entity-framework-4


    【解决方案1】:

    问题不在于您正在更改的任何值。事实上,您的新值在它们被设置的地方是不可为空的类型。

    由于失败的数据类型是 Int16,我将检查所有到 short 值的映射,以查找映射到可空列的任何不可空属性。

    【讨论】:

    • 我实际上也检查过。我进入实体表映射并强制这些值可以为空,但没有成功。似乎 Entity 的表映射没有显式设置 Nullable 属性,但该类型仍然作为 short? 来自应用程序,基于其在 Model.Designer.cs 中的定义。
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多