public void TestAlterEntity_ChangeTable() { using (Stream stream = Resources.GetXmlDocumentStream("suppressed")) { Assert.IsNotNull(stream); WXMLModel newModel = WXMLModel.LoadFromXml(new XmlTextReader(stream)); Assert.IsNotNull(newModel); Assert.AreEqual(2, newModel.GetActiveEntities().Count()); EntityDefinition e = newModel.GetActiveEntities().Single(item => item.Identifier == "e1"); Assert.AreEqual(1, e.GetSourceFragments().Count()); Assert.AreEqual(1, newModel.GetSourceFragments().Count()); Assert.AreEqual("tbl1", e.GetSourceFragments().First().Name); Assert.IsTrue(string.IsNullOrEmpty(e.GetSourceFragments().First().Selector)); SourceFragmentRefDefinition sf = new SourceFragmentRefDefinition( newModel.GetOrCreateSourceFragment("dbo", "table")); //foreach (SourceFragmentRefDefinition rsf in e.GetSourceFragments()) //{ // e.MarkAsDeleted(rsf); //} e.ClearSourceFragments(); e.AddSourceFragment(sf); foreach (PropertyDefinition property in e.GetProperties()) { property.SourceFragment = sf; } WXMLModel model = GetModel("suppressed"); model.Merge(Normalize(newModel)); e = model.GetActiveEntities().Single(item => item.Identifier == "e1"); Assert.AreEqual(1, e.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetSourceFragments().Count()); Assert.AreEqual("table", e.GetSourceFragments().First().Name); Assert.AreEqual("dbo", e.GetSourceFragments().First().Selector); e = model.GetActiveEntities().Single(item => item.Identifier == "e11"); Assert.AreEqual(1, e.GetSourceFragments().Count()); Assert.AreEqual("table", e.GetSourceFragments().First().Name); Assert.AreEqual("dbo", e.GetSourceFragments().First().Selector); } }
private void MergeEntities(WXMLModel mergeWith) { foreach (EntityDefinition newEntity in mergeWith.GetEntities()) { string newEntityIdentifier = newEntity.Identifier; EntityDefinition entity = GetEntities().SingleOrDefault(item => item.Identifier == newEntityIdentifier); if (entity != null) { if (!string.IsNullOrEmpty(newEntity.Name)) { entity.Name = newEntity.Name; } entity.Namespace = newEntity.Namespace; entity.BaseEntity = newEntity.BaseEntity; entity.Behaviour = newEntity.Behaviour; entity.CacheCheckRequired = newEntity.CacheCheckRequired; entity.Description = newEntity.Description; entity.Disabled = newEntity.Disabled; entity.InheritsBaseTables = newEntity.InheritsBaseTables; entity.AutoInterface = newEntity.AutoInterface; entity.UseGenerics = newEntity.UseGenerics; entity.FamilyName = newEntity.FamilyName; foreach (var @interface in newEntity.Interfaces) { if (!entity.Interfaces.ContainsKey(@interface.Key)) { entity.Interfaces.Add(@interface.Key, @interface.Value); } } entity.ClearSourceFragments(); foreach (SourceFragmentRefDefinition newsf in newEntity.OwnSourceFragments) { //string newsfId = newsf.Identifier; //SourceFragmentRefDefinition sf = // entity.GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfId); //foreach (PropertyDefinition rp in entity.GetProperties() // .Where(item=>item.SourceFragment == sf)) //{ // if (rp is ScalarPropertyDefinition) // { // ScalarPropertyDefinition property = rp as ScalarPropertyDefinition; // property.SourceField.SourceFragment = newsf; // } // else if (rp is EntityPropertyDefinition) // { // EntityPropertyDefinition property = rp as EntityPropertyDefinition; // property.SourceFragment = newsf; // } //} //entity.RemoveSourceFragment(sf); entity.AddSourceFragment(newsf); } //List<SourceFragmentRefDefinition> torem = new List<SourceFragmentRefDefinition>(); //foreach (SourceFragmentRefDefinition newsf in newEntity.GetSourceFragments()) //{ // string newsfId = newsf.Identifier; // SourceFragmentRefDefinition sf = // entity.GetSourceFragments().SingleOrDefault(item => item.Identifier == newsfId); // if (sf != null) // { // if (newsf.Action == MergeAction.Delete) // { // entity.RemoveSourceFragment(sf); // torem.Add(newsf); // } // else if (newsf.AnchorTable != null) // { // sf.AnchorTable = newsf.AnchorTable; // sf.JoinType = newsf.JoinType; // if (newsf.Conditions.Count > 0) // { // foreach (SourceFragmentRefDefinition.Condition c in newsf.Conditions) // { // SourceFragmentRefDefinition.Condition ec = // sf.Conditions.SingleOrDefault(item => // item.LeftColumn == c.LeftColumn && // item.RightColumn == c.RightColumn // ); // if (ec != null) // { // if (c.Action == MergeAction.Delete) // sf.Conditions.Remove(ec); // } // else // sf.Conditions.Add(c); // } // } // } // } // else // entity.AddSourceFragment(newsf); //} //foreach (SourceFragmentRefDefinition newsf in torem) //{ // newEntity.RemoveSourceFragment(newsf); //} foreach (PropertyDefinition newProperty in newEntity.GetProperties()) { string newPropertyName = newProperty.PropertyAlias; PropertyDefinition rp = entity.GetProperties().SingleOrDefault(item => item.PropertyAlias == newPropertyName); if (rp != null) { if (newProperty.Action == MergeAction.Delete) { entity.RemoveProperty(rp); } else { rp.Name = MergeString(rp, newProperty, item => item.Name); rp.ObsoleteDescripton = MergeString(rp, newProperty, item => item.ObsoleteDescripton); rp.DefferedLoadGroup = MergeString(rp, newProperty, item => item.DefferedLoadGroup); rp.GenerateAttribute = newProperty.GenerateAttribute; rp.AvailableFrom = MergeString(rp, newProperty, item => item.AvailableFrom); rp.AvailableTo = MergeString(rp, newProperty, item => item.AvailableTo); rp.Description = MergeString(rp, newProperty, item => item.Description); if (newProperty.Attributes != Field2DbRelations.None) { rp.Attributes = newProperty.Attributes; } rp.Feature = MergeString(rp, newProperty, item => item.Feature); rp.Group = newProperty.Group ?? rp.Group; rp.PropertyType = newProperty.PropertyType ?? rp.PropertyType; rp.Disabled = newProperty.Disabled; rp.EnablePropertyChanged = newProperty.EnablePropertyChanged; rp.SourceFragment = newProperty.SourceFragment ?? rp.SourceFragment; if (newProperty.FieldAccessLevel != default(AccessLevel)) { rp.FieldAccessLevel = newProperty.FieldAccessLevel; } if (newProperty.PropertyAccessLevel != default(AccessLevel)) { rp.PropertyAccessLevel = newProperty.PropertyAccessLevel; } if (newProperty.Obsolete != default(ObsoleteType)) { rp.Obsolete = newProperty.Obsolete; } if (rp.GetType() != newProperty.GetType()) { PropertyDefinition newProp = null; if (rp is EntityPropertyDefinition && newProperty is ScalarPropertyDefinition) { newProp = new ScalarPropertyDefinition(entity, newPropertyName); rp.CopyTo(newProp); } else if (rp is ScalarPropertyDefinition && newProperty is EntityPropertyDefinition) { newProp = new EntityPropertyDefinition(rp as ScalarPropertyDefinition); } entity.RemoveProperty(rp); entity.AddProperty(newProp); rp = newProp; } if (rp is ScalarPropertyDefinition) { ScalarPropertyDefinition property = rp as ScalarPropertyDefinition; property.SourceField.SourceType = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceType); property.SourceFieldAlias = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceFieldAlias); property.SourceField.SourceFieldExpression = MergeString(property, newProperty as ScalarPropertyDefinition, item => item.SourceFieldExpression); property.SourceField.IsNullable = ((ScalarPropertyDefinition)newProperty).IsNullable; property.SourceField.SourceTypeSize = ((ScalarPropertyDefinition)newProperty).SourceTypeSize ?? property.SourceTypeSize; } } } else { entity.AddProperty(newProperty); } } MergeExtensions(entity.Extensions, newEntity.Extensions); } else { AddEntity(newEntity); } } }