本文介绍了从KeyMembers中获取身份字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取 KeyMembers
,在其中将Edmx中的 StoreGeneratedPattern
设置为身份
有办法吗?
I would like to get the KeyMembers
where I have set in the Edmx the StoreGeneratedPattern
to Identity
is there a way to do this?
我可以使用以下代码获取KeyMembers:
I can get the KeyMembers with this code:
private static IEnumerable<EdmMember> GetKeyMembers(string entityName)
{
var objectContext = EntityModel.ObjectContext;
var metaData = objectContext
.MetadataWorkspace
.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets
.FirstOrDefault(x => x.ElementType.Name == entityName);
return metaData.ElementType.KeyMembers;
}
我遇到的问题是它也返回外键,但是我只是想要获取具有自动递增值的PrimaryKeys。
The problem I have with this that it also returns foreign keys, but I just want to get PrimaryKeys with auto incrementing values.
推荐答案
从给定的 MetadataWorkspace 中,我们将其称为 mw ,我用这个:
From a given MetadataWorkspace, let's call mw, i use this:
var cSpaceEntities = mw.GetItems(DataSpace.CSpace).OfType<EntityType>();
foreach (var entity in cSpaceEntities) {
var autoIds = entity.KeyMembers.Where(p =>
p.MetadataProperties
.Any(m => m.PropertyKind == PropertyKind.Extended
&& Convert.ToString(m.Value) == "Identity")).ToArray();
}
请测试
这篇关于从KeyMembers中获取身份字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!