简体   繁体   中英

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'

I have a MySQL stored procedure selecting data from specific table named tuser .

I'm using EntityFramework6, so I defined the result of procedure as an entity of tuser .

在此处输入图片说明

When I use the procedure in C# code, the following exception is thrown:

The 'bIsActive' property on 'tuser' could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Boolean'.

I cannot understand connection between the action I want to do and exception thrown.

Table definition in Database:

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;

bIsActive definition in ef:

在此处输入图片说明

store procedure definition:

CREATE DEFINER=``@`` PROCEDURE `GetActiveUsers`()
BEGIN
  select u.* from tuser u
  where u.bIsActive=true;
END

error occures when executing following code line:

List<tuser> list = Context.GetActiveUsers().ToList();

inner GetActiveUsers code (auto generated):

public virtual ObjectResult<tuser> GetActiveUsers()
{    
   return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<tuser>("GetActiveUsers");
}

Ok, Thanks to all the responders.

I still don't know what was the problem, but when I removed tuser table from model and added it again, problem has been solved.

The exception tells us that the boolean variable bIsActive is being assigned a decimal or not being cast properly somewhere around the following line:

List<tuser> list = Context.GetActiveUsers().ToList();

The solution is therefore a journey from the point the exception is thrown, to the point where the value has been misassigned.

To begin with, try changing...

public virtual ObjectResult<tuser> GetActiveUsers()
{    
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<tuser>("GetActiveUsers");
}

...with...

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 + ")");

    }

}

I hope you can see what I'm trying to do there ie split up the code into bite-sized chunks and study the output of each one. The message box message tells you if an exception was thrown or not, and will give a StackTrace , which is very helpful on a journey like this.

  • Replace the method
  • Run the program
  • Paste a screenshot of the MessageBox into your post

The next step will probably be to take a look at the database function, GetActiveUsers .

How do you manage your MySql database? Do you use MySql workbench or another interface? You should be able to view the function from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM