Code to get SQL table and column schema from Entity Framework

 

public static class Map
 {
 static Map()
 {
 var doc = new XmlDocument();
 using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("src.Admin.Entities.Model.msl"))
 doc.Load(stream);
//"urn:schemas-microsoft-com:windows:storage:mapping:CS"
var nodes = doc.SelectNodes("EntityTypeMapping").Cast<XmlNode>();
 LookupTableByType = nodes.ToDictionary(
 n => (NodeType)Enum.Parse(typeof(NodeType), n["TypeName"].Value.Split('.')[1], true),
 n => n.SelectSingleNode(@"MappingFragment/@StoreEntitySet']").Value
 );
 Tables = nodes.ToDictionary(
 n => (NodeType)Enum.Parse(typeof(NodeType), n["TypeName"].Value.Split('.')[1], true), 
 n => n.SelectNodes("ScalarProperty")
 .Cast<XmlNode>()
 .ToDictionary(
 s => s["Name"].Value,
 s => s["ColumnName"].Value
 )
 );
 }
 public static readonly Dictionary<NodeType, string> LookupTableByType;
 public static readonly Dictionary<NodeType, Dictionary<string, string>> Tables;
 }

May 5, 2013 В· admin В· No Comments
Tags: , , ,  В· Posted in: MSSQL, Tech

Leave a Reply