![](/img/trans.png)
[英]could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Double'
[英]The 'x' property on 'y' could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Boolean'
我有一個MySQL存儲過程,該過程從名為tuser
特定表中選擇數據。
我正在使用EntityFramework6,因此我將過程的結果定義為tuser
的實體。
當我在C#代碼中使用該過程時,將引發以下異常:
無法將“ tuser”上的“ bIsActive”屬性設置為“ System.Decimal”值。 您必須將此屬性設置為“ System.Boolean”類型的非空值。
我無法理解我要執行的操作與引發的異常之間的聯系。
數據庫中的表定義:
CREATE TABLE `tuser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sUserName` varchar(45) DEFAULT NULL,
`sUserNameMail` varchar(45) DEFAULT NULL,
`sMail` varchar(45) DEFAULT NULL,
`bIsActive` bit(1) DEFAULT b'1')
ENGINE=InnoDB AUTO_INCREMENT=2225 DEFAULT CHARSET=utf8;
ef中的bIsActive定義:
存儲過程定義:
CREATE DEFINER=``@`` PROCEDURE `GetActiveUsers`()
BEGIN
select u.* from tuser u
where u.bIsActive=true;
END
執行以下代碼行時發生錯誤:
List<tuser> list = Context.GetActiveUsers().ToList();
內部的ActiveActiveUsers代碼(自動生成):
public virtual ObjectResult<tuser> GetActiveUsers()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<tuser>("GetActiveUsers");
}
好的,感謝所有響應者。
我仍然不知道問題出在哪里,但是當我從模型中刪除tuser表並再次添加時,問題已經解決了。
該異常告訴我們,布爾變量bIsActive
被分配了decimal
或未正確地轉換為以下行附近的某處:
List<tuser> list = Context.GetActiveUsers().ToList();
因此,解決方案是從拋出異常的點到錯誤分配值的過程。
首先,嘗試更改...
public virtual ObjectResult<tuser> GetActiveUsers()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<tuser>("GetActiveUsers");
}
...與...
public virtual ObjectResult<tuser> GetActiveUsers()
{
try
{
Stringbuilder sb = new Stringbuilder();
IObjectContextAdapter a = ((IObjectContextAdapter)this); // Breakpoint this line and F11 step through the code, looking at local variables as you go
sb.AppendLine(", a: " + a.ToString());
var b = a.ObjectContext;
sb.AppendLine(", b: " + b.ToString());
var c = b.ExecuteFunction<tuser>("GetActiveUsers");
sb.AppendLine(", c: " + c.ToString());
MessageBox.Show("No exception: (" + sb.ToString() + ")");
return c;
}
catch(Exception ex)
{
MessageBox.Show("Exception: (" + ex.message + ex.stacktrace + ")");
}
}
我希望您能看到我正在嘗試做的事情,即將代碼分成幾小塊,並研究每個代碼的輸出。 消息框消息會告訴您是否引發了異常,並將給出StackTrace
,這對於像這樣的旅程非常有幫助。
下一步可能是看看數據庫函數GetActiveUsers
。
您如何管理MySql數據庫? 您使用MySql工作台還是其他接口? 您應該能夠從那里查看該功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.