This is a migrated thread and some comments may be shown as answers.

How do I get ID of new inserted Object

10 Answers 310 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paul
Top achievements
Rank 1
Paul asked on 20 May 2009, 04:36 PM
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.

 

10 Answers, 1 is accepted

Sort by
0
Alfred Ortega
Top achievements
Rank 2
answered on 20 May 2009, 08:29 PM
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.
0
Alexander
Telerik team
answered on 21 May 2009, 08:20 AM
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.
0
Paul
Top achievements
Rank 1
answered on 21 May 2009, 08:23 AM
Hi,
    Thats great thanks.
0
Paul
Top achievements
Rank 1
answered on 21 May 2009, 03:46 PM
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.


0
Paul
Top achievements
Rank 1
answered on 21 May 2009, 04:44 PM
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?
0
Alexander
Telerik team
answered on 24 May 2009, 07:41 PM
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.
0
Zbigniew Kozłowski
Top achievements
Rank 1
answered on 20 Apr 2010, 02:03 PM
i think the problem is still there ive just created mysql database, than reverse mapping and i dont have autoinc.

Regards,
Zbigniew Kozłowski
0
Alexander
Telerik team
answered on 21 Apr 2010, 12:27 PM
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.
0
Håvard
Top achievements
Rank 1
answered on 07 May 2010, 11:23 AM
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.
0
Serge
Telerik team
answered on 10 May 2010, 05:17 PM
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.
Tags
Getting Started
Asked by
Paul
Top achievements
Rank 1
Answers by
Alfred Ortega
Top achievements
Rank 2
Alexander
Telerik team
Paul
Top achievements
Rank 1
Zbigniew Kozłowski
Top achievements
Rank 1
Håvard
Top achievements
Rank 1
Serge
Telerik team
Share this question
or