locked
Unable to add record, Database Identity value required? RRS feed

  • Question

  • Configuration (3 separate solutions)

    1. Lightswitch HTML application
    2. OData service with controllers/routing
    3. POCO model with attached .mdf containing single test table with 3 fields:
    • Id – primary key, identity, not nullable

    [Key]

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int Id { get; set; }

    • FirstName
    • LastName

    Software/Platform:

    1. Visual Studio 2013
    2. OData v3
    3. EF v6
    4. Windows7 Pro 64

    Problem:

    I have 3 screens (AddEdit, Browse, View) in the application based on the test table. The auto-generated model of the test table marks the Id field as required. When I try to add a new entry in the Add/Edit screen, I get a validation error pertaining to missing a value for the Id.

    Why isn't the application picking up the model annotation?

    DatabaseGenerated(DatabaseGeneratedOption.Identity)

    Is this a bug or am I not setting something up?

    Thanks, Vanohrmer


    • Edited by vanohrmer Wednesday, January 7, 2015 9:29 PM
    Tuesday, January 6, 2015 7:10 PM

Answers

  • I figured out a work around for this. I added the following:

    myapp.AddEditKVGTable.created = function (entity) {

        // Write code here.

        entity.KVGTable.Id = 1;

    };

    This satisfies the validation on the client. Then controller seems to be aware of the annotation...not only does it not insert the default value from the client but it passes back the new key from the database.

    Thanks


    • Marked as answer by vanohrmer Sunday, January 11, 2015 12:27 AM
    • Edited by vanohrmer Sunday, January 11, 2015 6:32 PM
    Sunday, January 11, 2015 12:27 AM

All replies

  • >> DatabaseGenerated(DatabaseGeneratedOption.Identity)

    This represents the pattern used to generate values for a property in the database. you can use trace your Lightswitch application, it can provide more information about this issue.

    Friday, January 9, 2015 8:07 AM
  • I figured out a work around for this. I added the following:

    myapp.AddEditKVGTable.created = function (entity) {

        // Write code here.

        entity.KVGTable.Id = 1;

    };

    This satisfies the validation on the client. Then controller seems to be aware of the annotation...not only does it not insert the default value from the client but it passes back the new key from the database.

    Thanks


    • Marked as answer by vanohrmer Sunday, January 11, 2015 12:27 AM
    • Edited by vanohrmer Sunday, January 11, 2015 6:32 PM
    Sunday, January 11, 2015 12:27 AM
  • It sounds like your data model is messed up. Any chance you set IDENTITY on PK of table after you first attached to the external db?  If so, it's a bug in the update datasourse of LS.

    I've seen this with external tables that are attached to LS without a PK Identity.  Then when you alter the table in SQL and set IDENTITY(1,1) LS doesn't pick up that change with Update Datasource. 

    Assuming this is indeed your case, the fix is to clear out your generated artifacts then Update Data source and rebuild all.  As described by Hessc here:

     Lightswitch 2013 How to save nested add/edit screen data before coming back to first screen

    HTH,

    Josh


    • Edited by joshbooker Thursday, February 4, 2016 10:17 PM
    Thursday, February 4, 2016 10:15 PM