简体   繁体   中英

Entity Framework C# convert int to bool

I'm attempting to re-write a VB.NET WebForms application in C# MVC. I'm having an issue with one of the properties when using Entity Framework to instantiate a class.

I have a column in my database "VATInclusive", which is of type 'int'. The original application implicitly converted a "1" or "0" to "true" or "false", but when trying to do this in my application, I get the following error:

The 'VATInclusive' property on 'Shop' could not be set to a 'System.Int32' value. You must set this property to a non-null value of type 'System.Boolean'.

I can't simply change the type in the database as other applications make use of the table. I've tried using the following code to convert the value, but it seems to only return false, regardless of whether the database has a "0" or a "1"... Can anybody suggest a solution to this?

    [Column("VATInclusive")]
    private int _VATInclusive { get; set; }

    [NotMapped]
    public bool VATInclusive
    {
        get
        {
            if (_VATInclusive == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        set 
        {
            if(_VATInclusive == 0)
            {
                this.VATInclusive = false;
            }
            else 
            {
                this.VATInclusive = true;
            }
        }
    } 

Following some advice from the answers provided, I have rectified the issue. The issue lay with the setter accessor and also with the _VATIncusive property. By changing the code to the following I have managed to get the system to work as I expected.

However, I feel that this isn't the best approach, but it appears to be working correctly...

EDIT : EDIT : I've reduced the get accessor as per advice from Ryan and hvd..

EDIT : I'm not sure of the implications of having both properties set to public. But I don't think this is going to be an issue.

    [Column("VATInclusive")]
    public int _VATInclusive { get; set; }

    [NotMapped]
    public bool VATInclusive
    {
        get
        {
            return _VATInclusive != 0;
        }
        set 
        {
            _VATInclusive = value ? 1 : 0;
        }
    }

如果将列存储为位,Entity Framework会自动将其作为布尔值查询。

You have some typos on your setter. I think you mean for it to be:

set 
    {
        if(value == false)
        {
            _VATInclusive = 0;
        }
        else 
        {
            _VATInclusive = 1;
        }
    }

Basically, "value" represents the bool value passed in to your setter (to be converted in to an integer). _VATInclusive is the actual object that you want to be modifying under-the-hood.

You can't have a setter accessor assign to itself - this will always result in a StackOverflowException. In the below code:

set 
{
    if(_VATInclusive == 0)
    {
        this.VATInclusive = false;
    }
    else 
    {
        this.VATInclusive = true;
    }
}

every time this.VATInclusive is assigned to, the control flow returns to the beginning of the set accessor. This obviously can never complete.

在您的set ,您需要与value进行比较:

if (value == 0)

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