定义计算列的数据类型 [英] Define Data Type of Computed Column

查看:72
本文介绍了定义计算列的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指定的计算列,如下所示:

I have a computed column specified as follows:

(case when [StartDate]<=getdate() AND [EndDate] IS NULL then (1) else (0) end)

我正在尝试使用实体框架中此列的值。是否有可能将其识别为 boolean 的方法?那,或者我可以根据它的返回值在数据库中将它定义为一点吗?就这样,Entity Framework认为它是 int (似乎在不清楚的时候也会这样做)。

I'm trying to use the value of this column in Entity Framework. Is there any possible way to have this recognized as a boolean? That, or can I have it defined as a bit in my database, based on its return values? As it is, Entity Framework considers it an int (as it seems to do whenever it's not clear).

推荐答案

只需将值强制转换为 bit ,就像这样:

Simply cast the value to bit, like this:

CAST ((case when [StartDate]<=getdate() AND [EndDate] IS NULL 
       then (1) else (0) end) AS BIT)

EF会自动将该列识别为布尔值。

EF will automatically recognize this column as boolean.

实际上原始查询返回一个整数,这就是EF将其识别为整数的原因。在SQL Server中,除非您相反地说,否则1和0是整数值。如果要SQL Server识别它们,则必须使用 CAST(0作为BIT) CAST(1作为BIT)作为布尔(位)类型。

In fact the original query returns an integer, and that's why EF recognizes it as integer. In SQL Server, unless you say the contrary, 1 and 0 are integer values. You must use CAST (0 as BIT) or CAST (1 as BIT) if you want SQL Server to recognize them as boolean (bit) type.

这篇关于定义计算列的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆