public void OnChange() { var cfg_name = this.Name + "_EF_Edmx"; var cfg = System.Configuration.ConfigurationManager.AppSettings[cfg_name]; if (cfg == null) { DBGlobalService.EFConceptualModels = null; DBGlobalService.EFStorageModels = null; return; } if (!System.IO.File.Exists(cfg)) { DBGlobalService.EFConceptualModels = null; DBGlobalService.EFStorageModels = null; return; } EF.StorageModels.Schema ef_storage = new EF.StorageModels.Schema(); EF.ConceptualModels.Schema ef_concept = new EF.ConceptualModels.Schema(); EF.Mappings.Mapping mapping = new EF.Mappings.Mapping(); try { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(cfg); var schema_storage = doc.ChildNodes[1].ChildNodes[1].ChildNodes[1].ChildNodes[0]; ef_storage.Parse(schema_storage); var schema_concept = doc.ChildNodes[1].ChildNodes[1].ChildNodes[3].ChildNodes[0]; ef_concept.Parse(schema_concept); var mappingNode = doc.ChildNodes[1].ChildNodes[1].ChildNodes[5].ChildNodes[0]; mapping.Parse(mappingNode); DBGlobalService.EFStorageModels = ef_storage; DBGlobalService.EFConceptualModels = ef_concept; DBGlobalService.EFMappings = mapping; EFManager.Instance().Reset(); } catch (Exception e) { //MessageBox.Show(e.Message); return; } }
private void CompareEFDesign_Click(object sender, EventArgs args) { EF.StorageModels.Schema ef_storage = new EF.StorageModels.Schema(); EF.ConceptualModels.Schema ef_concept = new EF.ConceptualModels.Schema(); try { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); var cfg = System.Configuration.ConfigurationManager.AppSettings["EF_Edmx"]; doc.Load(cfg); var schema_storage = doc.ChildNodes[1].ChildNodes[1].ChildNodes[1].ChildNodes[0]; ef_storage.Parse(schema_storage); var schema_concept = doc.ChildNodes[1].ChildNodes[1].ChildNodes[3].ChildNodes[0]; ef_concept.Parse(schema_concept); } catch (Exception e) { MessageBox.Show(e.Message); return; } if (this._tablePkgNode == null && this._tablePkgNode.Children.Count == 0) { MessageBox.Show("没有表"); return; } StringBuilder builder = new StringBuilder(); List <string> newTables = new List <string>(); //提示db新增加的表 foreach (var table in this._tablePkgNode.Children) { if (!ef_storage.EntityTypes.ContainsKey(table.Name)) { newTables.Add(table.Name); } } var index = 1; newTables.ForEach(x => builder.AppendLine( string.Format("数据库中新增的表:{0} {1}", index++, x) )); List <string> notExsitedTables = new List <string>(); //提示db不存在的表 ef_storage.EntityTypes.Keys.ToList().ForEach(x => { if (this._tablePkgNode.Children.Count(y => y.Name == x) == 0) { notExsitedTables.Add(x); } }); index = 1; notExsitedTables.ForEach(x => builder.AppendLine( string.Format("数据库中不存在的表:{0} {1}", index++, x) )); //提示db更新的表 var sharedTable = ef_storage.EntityTypes.Where( x => this._tablePkgNode.Children.Count( y => y.Name == x.Key) > 0).ToList(); foreach (var table in sharedTable) { var entityType = table.Value; var dbTable = this._tablePkgNode.Children.FirstOrDefault(x => x.Name == table.Key) as TableNode; //db多的字段 var newFields = new List <ColumnSchema>(); dbTable.GetColumnSetNode().Columns.ToList().ForEach(x => { if (!table.Value.Propertys.ContainsKey(x.Name)) { newFields.Add(x); } } ); index = 0; newFields.ForEach(x => builder.AppendLine( string.Format("数据表{0}新增的字段: {1} {2}", table.Key, x.Name, x.NativeType) )); //db少的字段 var noFields = new List <Property>(); entityType.Propertys.ToList().ForEach(x => { if (dbTable.GetColumnSetNode().Columns.Count(y => y.Name == x.Key) == 0) { noFields.Add(x.Value); } }); noFields.ForEach(x => builder.AppendLine( string.Format("数据表{0}缺少的字段: {1} {2}", table.Key, x.Name, x.Type) )); //db更新的字段 foreach (var property in entityType.Propertys) { var column = dbTable.GetColumnSetNode().Columns.FirstOrDefault(x => x.Name == property.Key); if (column == null) { continue; } if (property.Value.Nullable != column.AllowDBNull) { builder.AppendLine( string.Format("数据表{0}更改的字段: {1} {2}", table.Key, column.Name, column.AllowDBNull ? "数据库为可空" : "数据库为非空") ); } //if (property.Value.Type == "uniqueidentifier" && column.NativeType == "Guid") //{ // continue; //} var type = property.Value.Type; if (type.IndexOf("(") > 0) { type = type.Substring(0, type.IndexOf("(")); } if (type != column.NativeType) { builder.AppendLine( string.Format("数据表{0}更改的字段: {1} {2},数据库类型为{3}", table.Key, column.Name, type, column.NativeType)); } } } this.OnGenerate(this, builder); }
private void CompareEFDesign_Click(object sender, EventArgs args) { EF.StorageModels.Schema ef_storage = new EF.StorageModels.Schema(); EF.ConceptualModels.Schema ef_concept = new EF.ConceptualModels.Schema(); try { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); var cfg = System.Configuration.ConfigurationManager.AppSettings["EF_Edmx"]; doc.Load(cfg); var schema_storage = doc.ChildNodes[1].ChildNodes[1].ChildNodes[1].ChildNodes[0]; ef_storage.Parse(schema_storage); var schema_concept = doc.ChildNodes[1].ChildNodes[1].ChildNodes[3].ChildNodes[0]; ef_concept.Parse(schema_concept); } catch (Exception e) { MessageBox.Show(e.Message); return; } if (this._tablePkgNode == null && this._tablePkgNode.Children.Count == 0) { MessageBox.Show("没有表"); return; } StringBuilder builder = new StringBuilder(); List<string> newTables = new List<string>(); //提示db新增加的表 foreach (var table in this._tablePkgNode.Children) { if (!ef_storage.EntityTypes.ContainsKey(table.Name)) { newTables.Add(table.Name); } } var index = 1; newTables.ForEach(x => builder.AppendLine( string.Format("数据库中新增的表:{0} {1}", index++, x) )); List<string> notExsitedTables = new List<string>(); //提示db不存在的表 ef_storage.EntityTypes.Keys.ToList().ForEach(x => { if (this._tablePkgNode.Children.Count(y => y.Name == x) == 0) { notExsitedTables.Add(x); } }); index = 1; notExsitedTables.ForEach(x => builder.AppendLine( string.Format("数据库中不存在的表:{0} {1}", index++, x) )); //提示db更新的表 var sharedTable = ef_storage.EntityTypes.Where( x => this._tablePkgNode.Children.Count( y => y.Name == x.Key) > 0).ToList(); foreach (var table in sharedTable) { var entityType = table.Value; var dbTable = this._tablePkgNode.Children.FirstOrDefault(x => x.Name == table.Key) as TableNode; //db多的字段 var newFields = new List<ColumnSchema>(); dbTable.GetColumnSetNode().Columns.ToList().ForEach(x => { if (!table.Value.Propertys.ContainsKey(x.Name)) { newFields.Add(x); } } ); index = 0; newFields.ForEach(x => builder.AppendLine( string.Format("数据表{0}新增的字段: {1} {2}", table.Key, x.Name, x.NativeType) )); //db少的字段 var noFields = new List<Property>(); entityType.Propertys.ToList().ForEach(x => { if (dbTable.GetColumnSetNode().Columns.Count(y => y.Name == x.Key) == 0) { noFields.Add(x.Value); } }); noFields.ForEach(x => builder.AppendLine( string.Format("数据表{0}缺少的字段: {1} {2}", table.Key, x.Name, x.Type) )); //db更新的字段 foreach (var property in entityType.Propertys) { var column = dbTable.GetColumnSetNode().Columns.FirstOrDefault(x => x.Name == property.Key); if (column == null) continue; if (property.Value.Nullable != column.AllowDBNull) { builder.AppendLine( string.Format("数据表{0}更改的字段: {1} {2}", table.Key, column.Name, column.AllowDBNull ? "数据库为可空" : "数据库为非空") ); } //if (property.Value.Type == "uniqueidentifier" && column.NativeType == "Guid") //{ // continue; //} var type = property.Value.Type; if (type.IndexOf("(") > 0) { type = type.Substring(0, type.IndexOf("(")); } if (type != column.NativeType) { builder.AppendLine( string.Format("数据表{0}更改的字段: {1} {2},数据库类型为{3}", table.Key, column.Name, type, column.NativeType)); } } } this.OnGenerate(this, builder); }