我有一个 MVP 应用程序,它使用 EntityFramework 从 SQL DB 读取数据。该数据是从外部 CMS 网站数据库复制的,因此数据并不总是可靠的。
我读到的字段之一是 SQL 中的“位”数据类型,并且是 1 或 0,并且 ASP.NET MVC 希望它作为布尔值,否则我会收到关于将布尔值转换为 int 的错误。
问题是有时数据库中的位值可能为 NULL,这会使我的 MVC 应用程序崩溃。我无法检查 NULL,因为我收到布尔数据类型只能为真或假的错误。
例子
模型
namespace Foo.Models
{
[Table("provider")]
public class Bar
{
//can't check for null here, getter and setter only allow boolean
public Boolean FooBar { get; set; }
}
}
控制器
namespace Foo.Controllers
{
public ActionResult BarList()
{
//Can't check for null here, because if(Boolean == null) will always evaluate to false
List<Bar> bars = db.Bar.ToList();
return View(bars);
}
}
如果 FooBar 曾经为 NULL,则会在视图中抛出一个错误,即我不能在布尔值中包含 NULL。我该如何处理这种情况?