This project has moved and is read-only. For the latest updates, please go here.
1
Vote

Mappings are populated after running a first query

description

Hi. I'm using the useful EntityFrmaework.MappingAPI (V6.1.0.9) in a practice project. the problem is that when i first need to get a list of mappings using the library (before executing any first query against DbContext), it fails. this is not the case when i first run a query (e.g. Set<T>().ToList()) and then access to the Mapping API library to get some information about entity mappings (like FKs) for the first time.
EF version is 6.1.1 .

public class MyContext : DbContext{
            protected IEntityMap[] GetMappings()
    {
        return this.Db(); //using EntityFramework.MappingAPI.Extensions
    }

    protected IPropertyMap[] GetForeignKeys(IEntityMap map)
    {
        return map.Fks;
    }

    protected IPropertyMap[] GetMappingForeignKeys(Type entityType)
    {
        return GetMappingForType(entityType).Fks;
    }

    protected IEntityMap GetMappingForType(Type entityType)
    {
        return GetMappings().First(f => f.Type == entityType);
    }

    protected PropertyInfo[] GetForeignKeys(Type entityType)
    {
        return GetMappingForeignKeys(entityType).Select(x => entityType.GetReadWriteProperty(x.ColumnName)).ToArray();
    }
}
Thanks for the good API. You're a creative developer :)

comments

kanda wrote Dec 18, 2015 at 12:27 PM

I run into the similar problem.
My workaroud was to call this.Set<object>() before MappingAPI is used. It fitted well in DbContext.OnModelCreating method.