How to get schema name from entity structure?

I have the following code

using (WdmEntities context = new WdmEntities()) { //get object models from context ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext; var container = objContext.MetadataWorkspace.GetEntityContainer(objContext.DefaultContainerName, DataSpace.CSpace); List<string> schemas = new List<string>(); foreach (var set in container.BaseEntitySets) { foreach (var metaproperty in set.MetadataProperties) { //here if(metaproperty.Name == "Schema") { //but metaproperty.Value == NULL schemas.Add(metaproperty.Value); } } } } 

I get null values ​​instead of schema names. How can I get the shemas names from an entity frame. (In my db, I have two different masks). Maybe someone knows a different way?

+3
source share
2 answers

I had the wrong approach. The code below shows how I was able to get the name of the circuits.

 using (WdmEntities context = new WdmEntities()) { //get object models from context ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext; //get all full names types from object model var fullNameTypes = objContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.OSpace); /////////////////// var conStr = objContext.Connection.ConnectionString; var connection = new EntityConnection(conStr); var workspace = connection.GetMetadataWorkspace(); var entitySets = workspace.GetItems<EntityContainer>(DataSpace.SSpace).First().BaseEntitySets; for (int i = 0; i < fullNameTypes.Count; i++) { Type type = Type.GetType(fullNameTypes[i].FullName); string schema = entitySets[type.Name].MetadataProperties["Schema"].Value.ToString(); } } 
+2
source

Try it. It is well tested. Let me know if there is any doubt.

 using (var context = new WdmEntities()) { //get object models from context var objContext = ((IObjectContextAdapter)context).ObjectContext; var items = objContext.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace); var container = items != null ? items.First() : null; var schemas = new List<string>(); if(container != null) schemas = container.BaseEntitySets.Select(set => set.Name).ToList(); } 
+1
source

Source: https://habr.com/ru/post/1398260/


All Articles