处理外键类(EF)中的NullReferenceException [英] Handle NullReferenceException in Foreign Key Classes (EF)

查看:139
本文介绍了处理外键类(EF)中的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个小问题.问题是,在访问外键类时如何处理NullReferenceException,换句话说,请在调用/使用外键实例之前检查外键实例是否为null.例如:

Mammal.Human.FirstName-其中Human为null.但是我不想对每个外键类执行null检查.例如:Mammal.Dog.Age(现在Dog可以为null).

提前非常感谢!
亲切的问候,

Hi all,

I have a small problem. The thing is, how to handle a NullReferenceException when accessing foreign key classes, in other words, check if the foreign key instance is null before calling / using it. For example:

Mammal.Human.FirstName - Where Human will be null. But I don''t want to perform a null check for each foreign key class. For example: Mammal.Dog.Age (now Dog could be null).

Many thanks in advance!
Kind regards,

推荐答案

我认为有两个选择. 1)如果您有权访问类代码,则可能需要返回一个建议为null的字符串,例如未找到".
2)除了进行空检查以外,您真的没有其他选择.
I think there are two options. 1) if you have access to the classes code then you might want to return a string that suggests a null, e.g. "NOTFOUND".
2) You really don''t have any other option other than to do a null check.


这篇关于处理外键类(EF)中的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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