如何在 EntityKey 上编写通用主键查找器?
我认为下面的代码非常有用。但我尝试创建 EntityKEY。我用过:
EntityKey key = new EntityKey(entitySetKEYName, "XTableId", id);
But i dont want to use this. my table PRIMARYID YtableId,ZTableId, etc... How can i write this:
EntityKey key = new EntityKey(entitySetKEYName, Find().PrimaryKEY, id);
public T GetByPrimaryKey<T>(int id)
{
string entitySetName = _context.MetadataWorkspace.GetEntityContainer(_context.DefaultContainerName, DataSpace.CSpace).BaseEntitySets.
Where(q => q.ElementType.Name == typeof(T).Name).FirstOrDefault().Name;
string entitySetKEYName = string.Format("{0}.{1}", _context.DefaultContainerName, entitySetName);
EntityKey key = new EntityKey(entitySetKEYName, "XTableId", id);
return (T)_context.GetObjectByKey(key);
}
如何自动检测主键???????????????
i think that below codes is very useful. But i try to create EntityKEy. i used:
EntityKey key = new EntityKey(entitySetKEYName, "XTableId", id);
But i dont want to use this. my table PRIMARYID YtableId,ZTableId, etc... How can i write this:
EntityKey key = new EntityKey(entitySetKEYName, Find().PrimaryKEY, id);
public T GetByPrimaryKey<T>(int id)
{
string entitySetName = _context.MetadataWorkspace.GetEntityContainer(_context.DefaultContainerName, DataSpace.CSpace).BaseEntitySets.
Where(q => q.ElementType.Name == typeof(T).Name).FirstOrDefault().Name;
string entitySetKEYName = string.Format("{0}.{1}", _context.DefaultContainerName, entitySetName);
EntityKey key = new EntityKey(entitySetKEYName, "XTableId", id);
return (T)_context.GetObjectByKey(key);
}
HOW CAN AUTOMATICALLY DETECT PRIMARY KEY????????????????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,PK不一定是Int32,它可以是Guid甚至多属性复杂键,所以泛型方法应该除了Object之外作为PK...
这是我的相当丑陋的变体,但它完成了工作,尽管它需要适当的测试。
First,PK is not necessary Int32, it can be Guid or even multi property complex key, so generic method should except Object as PK...
Here is my pretty ugly variant, but it does the job, though it need proper testing.