简体   繁体   中英

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

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!

Would this work?

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

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM