Unable to set the ID of an instance

36 Views Asked by At

What is unique about my situation is that the ID's can not be randomly assigned so I set it's value within the instance. I created several instances of an entity using the modeler. Below is the XML created:

  <cf:entity name="Test4" namespace="Amikids.TimeTracking" categoryPath="/Amikids.TimeTracking">
    <cf:property name="Id" key="true" typeName="int" />
    <cf:property name="Name" />
    <cf:instance>
      <cf:instanceValue name="Id">10</cf:instanceValue>
      <cf:instanceValue name="Name">Test 1</cf:instanceValue>
    </cf:instance>
    <cf:instance>
      <cf:instanceValue name="Id">20</cf:instanceValue>
      <cf:instanceValue name="Name">Test 2</cf:instanceValue>
    </cf:instance>
    <cf:instance>
      <cf:instanceValue name="Id">30</cf:instanceValue>
      <cf:instanceValue name="Name">Test 3</cf:instanceValue>
    </cf:instance>
  </cf:entity>

There are 2 things that are not working as expected:

  1. The records inserted do not use the ID specificed in the model/xml. Instead they were created incrementally starting at 1:

(The below is displayed in a code snippet only to prevent StackOverflow from reformatting my list so all records appear on one line)

ID Name
1  Test 1
2  Test 2
3  Test 3

  1. When I build the model a second time duplicate records are inserted.

(The below is displayed in a code snippet only to prevent StackOverflow from reformatting my list so all records appear on one line)

    ID Name
    1  Test 1
    2  Test 2
    3  Test 3
    4  Test 1
    5  Test 2
    6  Test 3

1

There are 1 best solutions below

0
On

Although specifying the ID in the instance does not appear to be work, as a simple work around I created the records using code, which allowed me to specify the ID. This has been verified with the following code snippet.

         Amikids.TimeTracking.Test4 test4  = new Amikids.TimeTracking.Test4();
         test4.Id = 100;
         test4.Name = "Test 100";
         test4.Save();

         test4 = new Amikids.TimeTracking.Test4();
         test4.Id = 200;
         test4.Name = "Test 200";
         test4.Save();