看到EF6模型检查财产,如果它有一个价值或没有? [英] Checking property of EF6 model to see if it has a value or not?
问题描述
我试图找出如何检查我的EF 6模型的属性,看它是否包含一个值或没有。该物业是一个Int64,所以我不能用的String.Empty
,我不能只把它比作与出一个空字符串转换它。我怎么能修改此检查,因此它会返回否,如果没有价值LogoFileID?
HasLogo =(section.LogoFileID!=的String.Empty)? 是:否;
下面是我的模型
公共类科
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)
公众的Int16 ID {搞定;组; } 公众的Int64? LogoFileID {搞定;组; } [必需的,最大长度(250),列(类型名=VARCHAR)]
公共字符串RouteName {搞定;组; } [必需的,最大长度(15),列(类型名=VARCHAR)]
公共字符串类型{搞定;组; } [需要]
公共字符串名称{搞定;组; } 公共字符串概要{搞定;组; } [ForeignKey的(LogoFileID)]
公共虚拟文件标识{搞定;组; }
}
HasLogo =(section.LogoFileID.HasValue)? 是:否;
您正在使用一个空Int64的类型,这样一个HasValue属性暴露给你想要的东西。
有关可空类型概述文档: http://msdn.microsoft.com/ EN-US /库/ 1t3y8s4s.aspx
I am trying to figure out how to check a property of my EF 6 model to see if it contains a value or not. The property is an INt64 so I can't use string.Empty
and I can not just compare it to an empty string with out converting it. How can I modify this check so it will return "No" if there is no value in "LogoFileID"?
HasLogo = (section.LogoFileID != string.Empty) ? "Yes" : "No";
Here is my model
public class Section
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int16 ID { get; set; }
public Int64? LogoFileID { get; set; }
[Required, MaxLength(250), Column(TypeName = "varchar")]
public string RouteName { get; set; }
[Required, MaxLength(15), Column(TypeName = "varchar")]
public string Type { get; set; }
[Required]
public string Title { get; set; }
public string Synopsis { get; set; }
[ForeignKey("LogoFileID")]
public virtual File Logo { get; set; }
}
HasLogo = (section.LogoFileID.HasValue) ? "Yes" : "No";
You're using a nullable int64 type so a HasValue property is exposed giving you what you want.
Documentation for nullable type overview: http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
这篇关于看到EF6模型检查财产,如果它有一个价值或没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!