我正在使用 Oracle Managed Data Access 组件 代码优先
正在尝试将 Int16 转换为 bool 值 但总是收到异常
'The 'property name' property on 'class name' could not be set to 'System.Int16' value. You must set this property to a non-null value of the 'System.Boolean'
如果我在我的 POCO 中有一个 bool 值作为属性并使用 DbSet 的 SqlQuery 或手动创建一个数据读取器,我会收到一个转换异常
有趣的是,如果我使用普通的 EF,例如
var test = dbContext.Set<Person>().Where(c=> 1==1).ToList();
没有抛出异常,并且设置了预期的属性值。
基本模型
public class Person
{
[Key]
public int Id { get; set; }
public bool Active { get; set; }
}
调用 ToList 引发异常
static void Main(string[] args)
{
var dbContext = new Context();
var sql = "select 1 Id, cast(1 as Number(1,0)) Active from dual";
var query = dbContext.Set<Person>().SqlQuery(sql);
var list = query.ToList();
}
我在我的配置文件中定义了 edmMappings(我认为这不重要)如下:
<oracle.manageddataaccess.client>
<version number="*">
<edmMappings>
<edmNumberMapping>
<add NETType="bool" MinPrecision="1" MaxPrecision="1" DBType="Number" />
<add NETType="byte" MinPrecision="2" MaxPrecision="3" DBType="Number" />
<add NETType="int16" MinPrecision="4" MaxPrecision="5" DBType="Number" />
<add NETType="int32" MinPrecision="6" MaxPrecision="10" DBType="Number" />
<add NETType="int64" MinPrecision="11" MaxPrecision="19" DBType="Number" />
</edmNumberMapping>
</edmMappings>
</version>
</oracle.manageddataaccess.client>
我的最终目标是避免修改我的 POCO
最佳答案
也许你可以如下声明你的实体类来实现你需要的
public class Person
{
[Key]
public int Id { get; set; }
public int Active { get; set; }
[NotMapped]
public bool IsActive
{
get { return Convert.ToBoolean(Active); }
set { Active = Convert.ToInt32(value); }
}
}
希望这对您有所帮助。
关于c# - 无法将 Number(1,0) 转换为 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39778601/