如何确定实体框架对象是实体还是复杂类型? [英] How do I determine whether an entity framework object is an entity or a complex type?

查看:76
本文介绍了如何确定实体框架对象是实体还是复杂类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所问,是否有办法判断给定对象是复杂类型还是实体?



这是我当前的代码,其中ent是对象上下文:



As the question asks, is there a way to tell whether a given object is a complex type or an entity?

This is my current code where ent is the object context:

var objectResult = (from meta in ent.MetadataWorkspace.GetItems(DataSpace.CSpace)
                                        .Select(m => m)
                                   from p in (meta as EntityType).Properties
                                       .Where(p => p.DeclaringType.Name == entity.GetType().Name)
                                   select p);

int count = objectResult.Count();





...如果对象不是实体类型,则对objectResult.Count()的调用会导致Object引用未设置为对象异常的实例。如果有必要,我可以捕获这个,但我希望还有另一种方法吗?



...the call to objectResult.Count() results in an Object reference not set to an instance of an object exception if the object is not an Entity type. I can trap this if necessary but am hoping there's another way around it?

推荐答案

这篇关于如何确定实体框架对象是实体还是复杂类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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