对象无法从 DBNull 转换为其他类型实体框架
我正在更新两个相关的实体对象,我们将它们称为 Order 和 OrderDetails。除了主字段之外的每个字段在数据库中都可以为空。
下面是一个代码片段:
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(对象 值,类型转换Type, 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](DbExpressionVisitor
1 游客) 在 System.Data.Mapping.Update.Internal.Propagator.Evaluator.Evaluate(DbExpression 节点、Propagator结果行、Propagator 家长) 在 System.Data.Mapping.Update.Internal.Propagator.Project(DbProjectExpression 节点、PropagatorResult 行、TypeUsage 结果类型) 在 System.Data.Mapping.Update.Internal.Propagator.Visit(DbProjectExpression 节点) 在 System.Data.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor
1 游客) 在 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() 在 System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable
1 命令、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:\Projects\Client\trunk\Client 应用程序\源\ClientProject\OrderPage.aspx.cs:行 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。
我一生都无法弄清楚为什么会发生这种情况。
I'm updating two related Entity objects lets call them Order and OrderDetails. Every field aside from the primary fields are nullable in the database.
Below is a code snippet:
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();
When the context goes to commit the changes to the db I get the following error:
System.InvalidCastException: Object
cannot be cast from DBNull to other
types.at System.DBNull.System.IConvertible.ToInt16(IFormatProvider
provider)
at System.Convert.ChangeType(Object
value, Type conversionType,
IFormatProvider provider)
at System.Data.Mapping.Update.Internal.Propagator.Evaluator.Cast(Object
value, Type clrPrimitiveType)
at System.Data.Mapping.Update.Internal.Propagator.Evaluator.Visit(DbCastExpression
node)
at System.Data.Common.CommandTrees.DbCastExpression.Accept[TResultType](DbExpressionVisitor1
1
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](DbExpressionVisitor
visitor)
at System.Data.Mapping.Update.Internal.Propagator.Propagate(UpdateTranslator
parent, EntitySet table,
DbQueryCommandTree umView)
at System.Data.Mapping.Update.Internal.UpdateTranslator.d_0.MoveNext()
at System.Linq.Enumerable.d_711.MoveNext()
1
at System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable
commands, UpdateTranslator translator)
at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
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.Objects.ObjectContext.SaveChanges()
at BSpace.InvoicePage.lbtn_CalculateDiscount_Click(Object
o, EventArgs e) in
C:\Projects\Client\trunk\Client
Application\Source\ClientProject\OrderPage.aspx.cs:line
1069
at System.EventHandler.Invoke(Object
sender, EventArgs e)
at System.Web.UI.WebControls.Button.OnClick(EventArgs
e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint)
In the database Orders.DiscountPercent is (float, null) and OrderDetails.ClientAmountReceived is (money, null).
Not sure why this is happening since these are the only two fields I'm updating. I've tried making sure there's data in every field and still get this error. I've also checked the data model file to make sure that the table mappings are correct for all associated fields.
OrderDetails.ClientAmountReceived translates to a Nullable and Order.DiscountPercent translates to a Nullable.
I can't for the life of me figure out why this is happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不在于您正在更改的任何值。事实上,您的新值在设置它们的位置处是不可为 null 的类型。
由于失败的数据类型是
Int16
,因此我将检查所有到short
值的映射,以查找映射到可为空列的任何不可为空属性。The problem is not in any of the values that you are changing. In fact, your new values are of non-nullable types at the places where they are being set.
Since the datatype that is failing is
Int16
, I would check all of the mappings toshort
values to look for any non-nullable properties mapped to nullable columns.