How can I get the type of an object from an EF EntitySet

c# entity-framework entity-framework-6

Question

I have created an EF convention that sets a schema name for a class based on the interface it implements directly or indirectly.

public class TableNameConvention<T> : IStoreModelConvention<EntitySet>
{
    private readonly string SchemaName;

    public TableNameConvention(string schemaName)
    {
        this.SchemaName = schemaName;
    }

    public virtual void Apply(EntitySet entitySet, DbModel model)
    {
        // Get the name of the Entity
        string name = entitySet.Name;

        // Check TEntityType Assembly for entitySet type
        Type type = typeof(T).Assembly.GetTypes().Where(x => x.Name == name).SingleOrDefault();

        // Check if type was found
        if (type != null)
        {
            // Check if type implements Type Parameter and if so, set schema
            if (typeof(T).IsAssignableFrom(type)) entitySet.Schema = SchemaName;
        }

        entitySet.Table = FormatName(name);
    }

This works fine 95% of the time. However, if there is another class with the same name anywhere in the Assembly, it'll return the default. I can change that to FirstOrDefault, but there's still no guarantee the first one is the one I'm actually looking for.

I know that without the fully qualified name, it's never going to be 100%, but does anyone have any suggestions to help improve the chances of getting the right type?

UPDATE

I changed the following:

Type type = AppDomain.CurrentDomain.GetAssemblies()
                       .SelectMany(t => t.GetTypes())
                       .Where(t => t.IsClass && t.Namespace == typeof(T).Namespace)
                       .FirstOrDefault(t => t.Name.Equals(name));

It'll only work if the derived class is in the same namespace as the base class, though. So if anyone has any other suggestions, please help!

1
1
4/1/2016 8:49:22 PM

Accepted Answer

The short answer is that there is no way to get the Entity's actual Type directly from an EF EntitySet, at least not in the Storage Model.

To make the code above work, I updated the class as follows:

private readonly string SchemaName;
private readonly string Namespace;

public TableNameConvention(string schemaName, string @namespace)
{
    this.SchemaName = schemaName;
    this.Namespace = @namespace;
}

and then updated where method on the Linq query to get the type from the AppDomain

Type type = AppDomain.CurrentDomain.GetAssemblies()
                   .SelectMany(t => t.GetTypes())
                   .Where(t => t.IsClass && t.Namespace == this.Namespace)
                   .FirstOrDefault(t => t.Name.Equals(name));
0
7/5/2016 8:05:23 PM

Popular Answer

Would this work?

        // Check TEntityType Assembly for entitySet type
        Type type = typeof(T).Assembly.GetTypes().Where(x => x == entitySet.GetType()).SingleOrDefault();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow