jeudi 15 février 2018

How to I create an Entity Framework ObjectSet using a generic type?

I am connecting to a database-first dll using Entity Framework 6.2.0 and I am trying to get the primary key for a given Entity at runtime. I don't know the Entity type until runtime, which is why I'm trying to use reflection to get the primary key.

Using the following, I'm getting the error Mapping and metadata information could not be found for EntityType 'System.Type':

        private string GetPrimaryKey<T>(T entity) where T : class
    {
        Context.DefaultContainerName = EFContainerName;
        var ESet = Context.CreateObjectSet<T>().EntitySet;
        return ESet.ElementType.KeyMembers.Select(k => k.Name).ToArray().First();
    }

I've seen a lot of information on the Mapping and Metadata error, but not with System.Type and so I feel like it may be less of a mapping error and more the way I'm using the Generic Type parameter?





Aucun commentaire:

Enregistrer un commentaire