views:

161

answers:

2

Hello, How can I get the 1st EntityKey name for an Entity for Entity Framework 4 because I'm building a repository system and I wanted to get an item by Id (which is the primary key oin EF is the 1st entitykey for the entity)

I'm using this code

public virtual TEntity GetById(string keyName, Guid entityId)
        {
            var entityKey = new EntityKey(this.QualifiedEntitySetName, keyName, entityId);
            object entity;
            return this.Context.TryGetObjectByKey(entityKey, out entity) ? entity as TEntity : null;
        }

I want to get the entity key name dynamic. Can anyone help me with this issue?

+2  A: 
Devart
+1  A: 

Guys I got it myself

var keyName = this.Context
    .MetadataWorkspace
    .GetEntityContainer(this.Context.DefaultContainerName, DataSpace.CSpace)
    .BaseEntitySets
    .First(meta => meta.ElementType.Name == this.entityName)
    .ElementType
    .KeyMembers
    .Select(k => k.Name)
    .FirstOrDefault();

I know it looks too much but u I wanted to get it by having the Entity Name.

Pr0fess0rX