Allow Identity Increment with out primary key association

11 posts, 0 answers
  1. Brian Cauley
    Brian Cauley avatar
    10 posts
    Member since:
    May 2006

    Posted 06 Dec 2009 Link to this post

    I am not able to find a way to allow one of my fields to be auto incremented without having to be a primary key, is this the case at this time?  If so, is this on the list of TODOs?
  2. Dimitar Kapitanov
    Admin
    Dimitar Kapitanov avatar
    632 posts

    Posted 09 Dec 2009 Link to this post

    Hi Brian Cauley,

     In reverse-mapping scenarios you should set a field mapped to such a column as read-only. On transaction commit and opening a new one, we will fetch such a value. In forward mapping scenario, you should add your script to generate such a column and execute that post our DDL script execution (currently there is no easy and straight forward way of doing that for forward mapping).


    Greetings,
    Dimitar Kapitanov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Brian Cauley
    Brian Cauley avatar
    10 posts
    Member since:
    May 2006

    Posted 13 Dec 2009 Link to this post

    Do you think you have that backwards?  I don't see a readonly field in the advanced view of the reverse mapping but I do see it in the forward mapping?  In the reverse mapping there is a Auto Increment but it doesn't see to allow me to set it, everytime I do it just reverts back to unchecked if it isn't a primary key.

    I have tried setting the forward mapping to readonly, can you use a combination of both forward and reverse? The error I am getting now when I commit a new object is "Access to unfilled state field DateAuction.Model.User:userID stateField=16".  I have the forward mapping for my userID set to readonly and it is a rowguid in the database.
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 15 Dec 2009 Link to this post

    Hello Brian Cauley,

    You could set the field behavior manually. This can be done by adding behavior="readonly" to the field node inside the corresponding App.config file.

    <field name="name" behavior="readonly"></field>

    Field which is of type uniqueidentifier in the database cannot be auto incremented. Identity for field of this type cannot be set. You should place custom logic for this field when you insert new record:
    userID = Guid.NewGuid();

    I hope that will help you.

    Sincerely yours,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Brian Cauley
    Brian Cauley avatar
    10 posts
    Member since:
    May 2006

    Posted 15 Dec 2009 Link to this post

    When I set this field manually will it be overwritten everytime the app.config file is update by the openaccess?

    As for the the auto incrementing I am aware of how the uniqueidentifier works. I have the userID set to a rowguid which does generate a new guid on an insert of a row.  The field I am trying to increment is an int, I guess I would have to set both of those fields to be readonly in the app.config in order for the insert to work correctly?
  7. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 16 Dec 2009 Link to this post

    Hello Brian Cauley,

    The settings inside the App.config file are overwritten only when they are changed from the wizards. The field behavior would be overwritten if you change it from the Forward Mapping wizard.

    Yes, you should set both of the fields as readonly in the App.config in order the insert to work correctly as well as you should set the integer field as identity.

    Kind regards,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Adam
    Adam  avatar
    30 posts
    Member since:
    Jun 2009

    Posted 28 Jan 2010 Link to this post

    I am having this same issue. I have a table that has a guid as the PK and an autoinc field to provide a more human readable id. When I try to insert into the table I get an error like this:

    Telerik.OpenAccess.Exceptions.DataStoreException: Error setting parameters on PreparedStatement for insert of '1573984092-de9c6ae2-937d-4415-860d-e5abf0a5c069':
    System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
       at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.setObject(Int32 parameterIndex, Object x, Int32 targetSqlType, Int32 scale)
       at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.setObject(Int32 parameterIndex, Object x, Int32 targetSqlType, Int32 scale)
       at OpenAccessRuntime.Relational.RelationalUtils.set_Renamed(PreparedStatement ps, Int32 index, Object val, Int32 javaTypeCode, Int32 jdbcType, RelationalColumn col)
       at OpenAccessRuntime.Relational.RelationalGenericOID.setParams(PreparedStatement ps, Int32 firstParam, RelationalColumn[] pkc)
       at OpenAccessRuntime.Relational.RelationalGenericState.setParams(PreparedStatement ps, Int32[] stateFieldNos, Int32 firstFieldNo, Int32 lastFieldNo, Int32 firstParam, PersistGraph pGraph, Int32 tableNo)
       at OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes)

    I then added the readonly attribute to the autoinc column and got error like this:

    2010-01-28 00:50:29,727 [ 21] ERROR ServiceErrorHandler - ProvideFault: Exception found.

    Telerik.OpenAccess.OpenAccessException: Specified cast is not valid. ---> System.InvalidCastException: Specified cast is not valid.

       at Telerik.OpenAccess.RT.StateManagerValueTypeWrapper.replacingGuidField(PersistenceCapable pc, Int32 field)

       at Services.DataModel.Element.OpenAccessEnhancedReplaceField(Int32 )

       at Services.DataModel.Element.OpenAccessEnhancedReplaceFields(Int32[] )

       at OpenAccessRuntime.DataObjects.PCStateMan.replaceApplicationPKFields()

       at OpenAccessRuntime.DataObjects.PCStateMan.commitImp(OpenAccessPersistenceManagerImp rpm)

       at OpenAccessRuntime.DataObjects.LocalPMCache.doCommit(Boolean retainValues)

       at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.internalCommit(Boolean phase)

       --- End of inner exception stack trace ---

       at Telerik.OpenAccess.RT.ExceptionWrapper.Throw()

       at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.handleException(Exception x)

       at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.internalCommit(Boolean phase)

       at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.commit()

       at OpenAccessRuntime.DataObjects.SynchronizedPMProxy.commit()

       at Telerik.OpenAccess.RT.TransactionImpl.Commit()



    If I remove the mapping for the autoinc column things work fine (but obviously I can't see Element_number in the entity, which I need).


  9. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 01 Feb 2010 Link to this post

    Hi Adam ,

    You should set both the fields as readonly and provide additional logic to generate the guid value inside the insert method:
    userID = Guid.NewGuid();

    Because a column of type uniqueidentifier cannot be auto incremented.
    Hope that helps.

    Regards,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  10. Brian Cauley
    Brian Cauley avatar
    10 posts
    Member since:
    May 2006

    Posted 01 Feb 2010 Link to this post

    Damyan,

    A uniqueidentity can be autogen by marking it as a rowguid. This will cause it to generate a new guid on insert. This does works on MS Sql 2008.
  11. Adam
    Adam  avatar
    30 posts
    Member since:
    Jun 2009

    Posted 01 Feb 2010 Link to this post

    Not sure you understood my post, I don't have a problem with the GUID field I have a problem with an int identity field.

    I have a table that has 2 IDs, one is a guid that is also the primary key. This GUID is generated in code. This works fine. We also have another column in the table that is an int identity field (not associated with the PK in any way). This is auto generated by the DB. When we map the int identity column and try to write to the table is when we get the errors I described above. I have tried putting readonly on the fields, as I described in my post, but still get errors.
  12. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 03 Feb 2010 Link to this post

    Hello Adam ,

    You could achieve this goal by doing the following steps:
    1. Run the Forward Mapping wizard and click on the class node
    2. On the right inside the Identity box select GUID as a Key Generator
    3. Set the field behavior of the identity int field to readonly
    When you finish click on the Done button.
    You could use the attached solution as well.
    I hope that helps.

    Kind regards,
    Damyan Bogoev
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
Back to Top
DevCraft banner