Esempio n. 1
0
        /// <summary>
        /// Gets table schema
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbCtx"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetTableSchema <T>(this System.Data.Entity.DbContext dbCtx, T obj)
        {
            var mappings = dbCtx.GetMappings(obj);

            // Find the storage entity set (table) that the entity is mapped
            var table = mappings
                        .EntityTypeMappings.Single()
                        .Fragments.Single()
                        .StoreEntitySet;

            // Return the table name from the storage entity set
            return((string)table.MetadataProperties["Schema"].Value ?? table.Schema);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets a mapped column name
        /// Based on https://romiller.com/2015/08/05/ef6-1-get-mapping-between-properties-and-columns/
        /// </summary>
        /// <param name="dbCtx"></param>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string GetTableColumnName <T>(this System.Data.Entity.DbContext dbCtx, T obj, string propertyName)
        {
            var mappings = dbCtx.GetMappings(obj);

            // Find the storage property (column) that the property is mapped
            var columnName = mappings
                             .EntityTypeMappings.Single()
                             .Fragments.Single()
                             .PropertyMappings
                             .OfType <ScalarPropertyMapping>()
                             .Single(m => m.Property.Name == propertyName)
                             .Column
                             .Name;

            return(columnName);
        }