无法设置实例的ID

时间:2016-12-08 18:10:16

标签: codefluent

我的情况的独特之处在于ID不能随机分配,因此我在实例中设置了它的值。我使用建模器创建了一个实体的几个实例。以下是创建的XML:

  <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>

有两件事情没有按预期发挥作用:

  1. 插入的记录不使用model / xml中指定的ID。相反,它们是从1开始逐步创建的:
  2. (以下内容仅显示在代码段中,以防止StackOverflow重新格式化我的列表,以便所有记录显示在一行上)

    ID Name
    1  Test 1
    2  Test 2
    3  Test 3

    1. 当我构建模型时,第二次插入重复记录。
    2. (以下内容仅显示在代码段中,以防止StackOverflow重新格式化我的列表,以便所有记录显示在一行上)

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

1 个答案:

答案 0 :(得分:0)

尽管在实例中指定ID似乎不起作用,但作为一个简单的解决方法,我使用代码创建了记录,这允许我指定ID。已使用以下代码段验证了这一点。

         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();