locked
How to edit a field that represents a PK? RRS feed

  • Question

  • Hi!

    I have in my tables one called Languages which has an Id column (PK, NOT NULL, nvarchar(50)). That Id represents the language code (es, en,...) and it's designed that way so tables that have a foreign key against Languages can save one inner join.

    When I create a screen to create a new Language, I can put the field as a TextBox, but it is impossible to write on it (while other fields are easily editable).

    Is there any way to edit a field that represents a PK? Regards!


    Vicente Cartas Espinel - MVP XNA/DirectX

    Blog about C# and XNA Development

    Blog about Role Playing Games

    Friday, March 22, 2013 11:49 PM

Answers

  • Hi Vicente,

    You should be able to edit a PK field for an entity that is being added. But once that entity is added, it's OData restriction that the PK field cannot be modified anymore.

    So the text box should be enabled when you're adding a new Language, but disabled when you're changing an existing Language.

    When you attach to a SQL Databases, LightSwitch should be able to look at the column definition and figure out whether that column has to be specified (Customers table in Northwind) or that column is auto-generated (Orders table in Northwind). Then LightSwitch will add the metadata to the model.

    For example if you attach to a Northwind database, build and open the model.json file under HTMLClient\GeneratedArtifacts, you would see this for the Customers entity

            {
              "__isEntityType": 1,
              ":@SummaryProperty": { "property": { "__id": "Customer/Properties[CustomerID]" } },
              "id": "Customer",
              "name": "Customer",
              "methods":
              [
                {
                  "id": "Customer/Methods[deleteEntity]",
                  "name": "deleteEntity"
                },
                {
                  "id": "Customer/Methods[edit]",
                  "name": "edit"
                },
                {
                  "id": "Customer/Methods[view]",
                  "name": "view"
                }
              ],
              "properties":
              [
                {
                  "__isKeyProperty": 1,
                  "id": "Customer/Properties[CustomerID]",
                  "name": "CustomerID",
                  "propertyType": { "__id": ":String" },
                  ":@Required": { },
                  ":@MaxLength":
                  {
                    "isFixedLength": true,
                    "value": 5
                  }
                },

    The metadata tells LightSwitch that CustomerID is a PK column, but it is not autogenerated.

    And here's the metadata for Orders table

            {
              "__isEntityType": 1,
              ":@SummaryProperty": { "property": { "__id": "Order/Properties[ShipName]" } },
              "id": "Order",
              "name": "Order",
              "methods":
              [
                {
                  "id": "Order/Methods[deleteEntity]",
                  "name": "deleteEntity"
                },
                {
                  "id": "Order/Methods[edit]",
                  "name": "edit"
                },
                {
                  "id": "Order/Methods[view]",
                  "name": "view"
                }
              ],
              "properties":
              [
                {
                  "__isKeyProperty": 1,
                  "id": "Order/Properties[OrderID]",
                  "name": "OrderID",
                  "isReadOnly": true,
                  "propertyType": { "__id": ":Int32" },
                  ":@Hidden": { },
                  ":@Required": { },
                  ":@NotSearchable": { }
                },
    

    The metadata tells LightSwitch that the OrderID is a PK column, but it's auto-generated and should not be edited by the Client.

    What is the behavior you're seeing with your Language table? Can you check the metadata and see if the PK column is auto-generated?

    Hope this helps. Best regards.
    Huy Nguyen

    • Proposed as answer by ADefwebserver Monday, March 25, 2013 6:03 PM
    • Marked as answer by Vicente Cartas Thursday, April 4, 2013 4:19 PM
    Monday, March 25, 2013 5:55 PM