Book a Demo

Author Topic: EA 13 and Elements.AddNew  (Read 6121 times)

Eamonn John Casey

  • EA User
  • **
  • Posts: 110
  • Karma: +0/-1
    • View Profile
EA 13 and Elements.AddNew
« on: November 26, 2016, 01:46:25 am »
I am having trouble with preventing adding duplicate elements in EA 13 Desktop Edition. The code below does not find the element (foreach). Also for some strange reason the SQLQuery does not return values. But in EA the same SQL returns values.

Any ideas? Is there something that I need to do on the repository til ensure updates are written?

Code: [Select]
internal Element FindOrCreateElement(Package rootPackage, string elementName, string stereoType)
{
    Element returnElement = null;

    foreach (Element element in rootPackage.Elements)
    {
        if (0 == element.Name.ToLower().CompareTo(elementName.ToLower())
            && 0 == element.Stereotype.ToLower().CompareTo(stereoType.ToLower()))
        {
            returnElement = element;
            break;
        }
    }

    if (null == returnElement)
    {
        returnElement = rootPackage.Elements.AddNew(elementName, stereoType);
        returnElement.Update();
    }
    return returnElement;
}

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: EA 13 and Elements.AddNew
« Reply #1 on: November 26, 2016, 02:47:23 am »
You are just looking in one package and not recursing. Try
Code: [Select]
Repository.GetElementSet("SELECT * FROM t_object WHERE Name = '...'") instead.

q.

Eamonn John Casey

  • EA User
  • **
  • Posts: 110
  • Karma: +0/-1
    • View Profile
Re: EA 13 and Elements.AddNew
« Reply #2 on: November 26, 2016, 10:41:27 pm »
Almost. But this bombs out too...

Code: [Select]
internal EA.Collection GetElementSet(int packageID, string stereoType)
{
    EA.Collection elements =
        eaRepository.GetElementSet(
        String.Format(
        "SELECT * FROM t_object WHERE Package_ID = {0} AND StereoType = {1}", packageID, stereoType), 0);

    return elements;
}

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: EA 13 and Elements.AddNew
« Reply #3 on: November 26, 2016, 11:05:05 pm »
You need to quote stereotype. It's a string.

q.