Question

I'm trying to write T4 templates to generate custom Views for my model classes, the model classes are located in another Assembly and the mappings are based on EF6 fluent API.

Currently, I'm using reflection to get properties' names and types, my question is how can I read the ModelConfiguration? , for example to get the Primary Key and related models' foreign keys.. etc.

Was it helpful?

Solution

samples access the Model. Note DataSpace has other Enum values

public void EFTools2Test()
    {
        var context = new MyContext("MYConnie");
        ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
        MetadataWorkspace workspace = objContext.MetadataWorkspace;
        IEnumerable<EntityType> managedTypes = workspace.GetItems<EntityType>(DataSpace.OSpace);
        var result = new List<Type>();
        foreach (var managedType in managedTypes) {

            Console.WriteLine(managedType.FullName);
            foreach ( var p in managedType.Properties) {
                Console.WriteLine(p.Name );
            }
        }


    }



    public void EFToolsTest() {
    //  http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.dataspace(v=vs.110).aspx
        var context = new MyContext("MYConnie");
        ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
        MetadataWorkspace workspace = objContext.MetadataWorkspace;

        var xyz = workspace.GetItems<EntityType>(DataSpace.SSpace);
        foreach (var ET in xyz) {
            foreach (var sp in ET.Properties) {
                Debug.WriteLine(sp.Name + ":" + sp.MaxLength);// just as an example

                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top