How do I get ID of new inserted Object

11 posts, 0 answers
  1. Paul
    Paul avatar
    113 posts
    Member since:
    May 2009

    Posted 20 May 2009 Link to this post

    Hi,
       On a Insert of a record in a MySQL db I can get the ID of that newly inserted record by LAST_INSERT_ID().

    How do I get the same thing when doing this.

    Persistant Class related to db table

     

     

      public partial class Application  
        {  
            //The 'no-args' constructor required by OpenAccess.   
            public Application()  
            {  
            }  
     
            [Telerik.OpenAccess.FieldAlias("applicationID")]  
            public int ApplicationID  
            {  
                get { return applicationID; }  
                set { this.applicationID = value; }  
            }  
     
            [Telerik.OpenAccess.FieldAlias("appPath")]  
            public string AppPath  
            {  
                get { return appPath; }  
                set { this.appPath = value; }  
            }  
     
            [Telerik.OpenAccess.FieldAlias("applicationName")]  
            public string ApplicationName  
            {  
                get { return applicationName; }  
                set { this.applicationName = value; }  
            }  
     

     Insert

            IObjectScope scope = ObjectScopeProvider.GetNewObjectScope();  
                scope.Transaction.Begin();  
                Application MyApp = new Application();  
     
                MyApp.ApplicationName = "TestApp";  
                MyApp.AppPath = "C:\Windows\TestApp";  
                scope.Add(MyApp);  
                scope.Transaction.Commit(); 

     
    How can I now get the new applicationID which is a primary autogenerated number. There could be more than record with TestApp so I can not do a simple select.

     

  2. Alfred Ortega
    Alfred Ortega avatar
    193 posts
    Member since:
    May 2005

    Posted 20 May 2009 Link to this post

    I don't think you have to do anything...

    After scope.Transaction.Commit() your applicationID should be populated as long as it is mapped to an auto-increment field.
  3. DevCraft banner
  4. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 21 May 2009 Link to this post

    Hi Paul,

    Yes, Alfred is right, after you execute scope.Transaction.Commit() the applicationID will contain the newly generated value.

    Best wishes,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Paul
    Paul avatar
    113 posts
    Member since:
    May 2009

    Posted 21 May 2009 Link to this post

    Hi,
        Thats great thanks.
  6. Paul
    Paul avatar
    113 posts
    Member since:
    May 2009

    Posted 21 May 2009 Link to this post

    Ok Spoke to soon. This didn't work.

    After adding it my applicationID was still 0

    I looked at my table in MySQL DB and confirmed that my applicationID column was a primary key and that it had Auto Increment ticked. So I looked at the reversemapping.config and found that this

    <

     

    column name="ApplicationID" isPk="True" isAutoInc="False" adoType="4" sqlType="INTEGER" length="0" isNull="False" />

     


    I went back into the reverse wizard to see if this option is available there but found nothing, So no way to check if it gets it wrong without looking at the config file. So is this a bug with Open access and Mysql? Also could we have a tick box for this next to the primary key one.

    I will try and edit the reversemapping.config manualy and set isAutoInc="True" to see if this makes it work.


  7. Paul
    Paul avatar
    113 posts
    Member since:
    May 2009

    Posted 21 May 2009 Link to this post

    Ok after manualy setting the following in the app.config for Application Table I got it to work

    <extension key="db-key-generator" value="AUTOINC" />

    So why is this not being done for me when the column in the table within the DB is a primary key and is autoInc?
  8. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 24 May 2009 Link to this post

    Hi Paul,

    It seems this is a bug with our MySql support. Autoincrement fields should be recognized automatically and that is why we do not have an option to specify this in the wizard. Sorry for the inconvenience, you will have to edit manually the configuration files until the fix is released. Your Telerik points have been updated.

    All the best,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Zbigniew Kozłowski
    Zbigniew Kozłowski avatar
    60 posts
    Member since:
    Apr 2010

    Posted 20 Apr 2010 Link to this post

    i think the problem is still there ive just created mysql database, than reverse mapping and i dont have autoinc.

    Regards,
    Zbigniew Kozłowski
  10. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 21 Apr 2010 Link to this post

    Hi Zbigniew Kozłowski,

    You are correct, OpenAccess is still not able to discover automatically the autoinc columns in MySql tables. It turned out that this is not a bug but missing functionality which we will add to our TODO list.
    However, we have added an Auto Increment check box in the Reverse mapping wizard which you can enable to mark a field as autoinc. This way the manual configuration modification is avoided.  This option can be used with Oracle as well.

    All the best,
    Alexander
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  11. Håvard
    Håvard avatar
    11 posts
    Member since:
    Dec 2009

    Posted 07 May 2010 Link to this post

    Any time frame on this feature?

    The reason I ask is because of the method I do to get db changes updated in the project. (I use reverse mapping)

    If I make a db change, I now delete all the files from my db model project except from the App.Config where I delete the namespace section and then do another reverse mapping. This was ok before, but if I will have to update all my tables with autoincrement this gets too time consuming.

    I realize I might be doing the update part wrong, but its the only reliable way I've figured out of getting my changes through.
  12. Serge
    Admin
    Serge avatar
    375 posts

    Posted 10 May 2010 Link to this post

    Hello Håvard,

    The suggested way of applying changes to the model is using the merge option. You can get a detailed overview of this functionality here. Using this approach you will not need to recreate everything and set the settings everywhere again.

    As to the fix, we have scheduled it for the next major release.

    Hope this helps.

    Regards,
    Serge
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top
DevCraft banner