AllmanTool
AllmanTool

Reputation: 1554

Entity Framework type conversion issue

I have this entity:

public class ResourceConfigPool
{
    public int ResourceId { get; set; }
    public bool TabOrder { get; set; }
}     

SQL Server table:

Column TABORDER char(1)

Request:

var tabOrder = _uow.Repository<ResourceConfigPool>().Get()
                    .Where(cf => cf.ResourceId == id)
                    .Select(cf => cf.TabOrder)
                    .SingleOrDefault(); 

And I get this error:

The specified cast from a materialized 'System.String' type to the 'System.Boolean' type is not valid

Question: how can I resolve this issue?

PS: I can't change the SQL Server side

Thanks in advance :)

Upvotes: 0

Views: 730

Answers (1)

John Woo
John Woo

Reputation: 263893

Since you can't change anything from SQL server side, you can try to create another property that will convert the value of the string. Example:

public class ResourceConfigPool
{
    public int ResourceId { get; set; }
    public string TabOrder { get; set; }
    public bool TabOrderBool 
    { 
        get { return TabOrder == "1"; } // or "Y"
    }
}  

then query query from the newly added property:

var tabOrder = _uow.Repository<ResourceConfigPool>().Get()
                    .Where(cf => cf.ResourceId == id)
                    .Select(cf => cf.TabOrderBool)
                    .SingleOrDefault();

Upvotes: 1

Related Questions