public static void SetValue(XElement _parent, PropertyExtensionContext _context, XName xName, string value) { string propertyValue = string.Empty; if (value != null) { propertyValue = value.Trim(); } using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set StarterKit Property")) { if (_parent.HasElements) { XElement lastChild = _parent.Elements().Where <XElement>(element => element != null && element.Name == xName).LastOrDefault(); if (lastChild != null) { lastChild.SetValue(propertyValue); } else { // MyNewProperty element does not exist, so create a new one as the last // child of the EntityType element. _parent.Elements().Last().AddAfterSelf(new XElement(xName, propertyValue)); } } else { // The EntityType element has no child elements so create a new MyNewProperty // element as its first child. _parent.Add(new XElement(xName, propertyValue)); } // Commit the changes. scope.Complete(); } }
public void SetValue(XElement parent, PropertyExtensionContext context, XName name, XElement value) { if (value == null) { foreach (var element in GetEntityTypesWithIdentityElement(parent, name)) { element.Element(name).Remove(); } return; } var otherElements = (from element in value.Elements() let otherName = element.Name where propertyNames.Contains(otherName) && otherName != name select element).ToList(); if (otherElements.Any()) { DialogResult result = DialogResult.OK; if (GetEntityTypesWithIdentityElement(parent, otherName).Count() == 1) { string nameString = name.LocalName.Substring(name.LocalName.Length - 5, 4); string otherNameString = otherName.LocalName.Substring(otherName.LocalName.Length - 5, 4); string message = string.Format("Selected entity type is already used as a {0} identity. Would you like to set it as a {1} identity? A {0} identity will be unset.", otherName, name); string caption = string.Format("Entity is already in use as {0} identity!", otherName); result = MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } if (result == DialogResult.Cancel) { return; } else { value.Element(otherName).Remove(); } } using (EntityDesignerChangeScope scope = context.CreateChangeScope("Set ASP.NET Identity Property")) { foreach (var element in GetEntityTypesWithIdentityElement(parent, name)) { if (element != value) { element.Element(name).Remove(); } } if (value.Elements(name).Count() < 1) { value.FirstNode.AddBeforeSelf(new XElement(name)); } // Commit the changes. scope.Complete(); } }
protected void SetProperty <T>(string propertyName, T propertyValue) { var propertyXName = XName.Get(propertyName, Namespace); // Make changes to the .edmx document in an EntityDesignerChangeScope to enable undo/redo of changes. using (var scope = Context.CreateChangeScope("Set CompositionRootProperty")) { //if (Parent.HasElements) //{ // var lastChild = Parent.Elements() // .Where(element => element != null && element.Name == propertyXName) // .LastOrDefault(); // if (lastChild != null) // { // // CompositionRootProperty element already exists under the EntityType element, so update its value. // lastChild.SetValue(propertyValue); // } // else // { // // CompositionRootProperty element does not exist, so create a new one as the last child of the EntityType element. // Parent.Elements().Last().AddAfterSelf(new XElement(propertyXName, propertyValue)); // } //} if (Parent.HasAttributes) { var attribute = Parent.Attributes() .Where(a => a != null && a.Name == propertyXName) .SingleOrDefault(); if (attribute != null) { // CompositionRootProperty element already exists under the EntityType element, so update its value. attribute.SetValue(propertyValue); } else { // CompositionRootProperty element does not exist, so create a new one as the last child of the EntityType element. Parent.SetAttributeValue(propertyXName, propertyValue); } } else { // The EntityType element has no child elements so create a new CompositionRootProperty element as its first child. Parent.Add(new XElement(propertyXName, propertyValue)); } // Commit the changes. scope.Complete(); } }
public void SetValue(XElement parent, PropertyExtensionContext context, string entityName, string identityType) { if (entityName == null) { using (EntityDesignerChangeScope scope = context.CreateChangeScope("Set ASP.NET Identity Property")) { RemoveIdentityAttributeOfType(parent, identityType); scope.Complete(); return; } } XElement set = GetEntitySetsByEntityName(parent, entityName).FirstOrDefault(); if (set == null) { throw new ArgumentException(string.Format("Entity Model does not contain any entity with name \"{0}\".", entityName)); } var identityTypeAttribute = set.Attributes(Constants.aspNetIdentityAttributeName).FirstOrDefault(); if (identityTypeAttribute != null) { if (identityTypeAttribute.Value == identityType) { return; } DialogResult result = DialogResult.OK; string otherType = identityTypeAttribute.Value; string message = string.Format("Selected entity type is already used as a {0} identity. Would you like to set it as a {1} identity? A {0} identity will be unset.", otherType, identityType); string caption = string.Format("Entity is already in use as {0} identity!", otherType); result = MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (result == DialogResult.Cancel) { return; } else { using (EntityDesignerChangeScope scope = context.CreateChangeScope("Set ASP.NET Identity Property")) { RemoveIdentityAttributeOfType(parent, identityType); identityTypeAttribute.Value = identityType; scope.Complete(); } } } else { using (EntityDesignerChangeScope scope = context.CreateChangeScope("Set ASP.NET Identity Property")) { if (!parent.Attributes(aspNetIdentityXmlnsAttributeName).Any()) { parent.Add(new XAttribute(aspNetIdentityXmlnsAttributeName, Constants.AspNetIdentityNamespace)); } identityTypeAttribute = new XAttribute(Constants.aspNetIdentityAttributeName, identityType); set.Add(identityTypeAttribute); scope.Complete(); } } }