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

[Solved] Get last inserted Id?

2 Answers 99 Views
Data Storage for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dirk
Top achievements
Rank 1
Iron
Dirk asked on 13 Sep 2013, 08:46 PM
In your DataStorageDemoXaml the database is created in the following way:
//table: Cities{CityID, CityName, Population} ; CityID field is autoincremental and auto generated
context.Insert(new Cities() { CityName = "New York", State = "New York", Population = 8300000 });
context.Insert(new Cities() { CityName = "Los Angeles", State = "California", Population = 3800000 });
 
//table: BikeStores {StoreID, CityFK, StoreName, StoreAddress}; StoreID field is autoincremental and auto generated
context.Insert(new BikeStores { CityFK = 1, StoreName = "Bikes for NY", StoreAddress = "8175 Thunder Bear Ramp" });

This is not realistic, even if it is a demo.
How can I know that the CityFK of New York is '1'?
How can I get the CityId of New York after inserting the dataset?

2 Answers, 1 is accepted

Sort by
0
Dirk
Top achievements
Rank 1
Iron
answered on 13 Sep 2013, 09:15 PM
Ok, I've found a solution.
context.Insert(new Cities() { CityName = "New York", State = "New York", Population = 8300000 });
context.SaveChanges();
string query = "select last_insert_rowid() as id from Cities";
var lastId = context.GetScalar<int>(query);

I have always to call SaveChanges and use the special query to get the last Id.
Is there no better way implemented?



0
Bulent
Telerik team
answered on 16 Sep 2013, 12:24 PM
Hi Dirk,

Thank you for the good question.

Currently, we do not support database schema and particularly table relationships and foreign keys. That's why the developers should use some workarounds in order to maintain relations between tables. Your solution is good enough and it solves the issue.
We will add your request in our backlog and we will consider some improvements to simplify this scenario. 

Please, let me know should you have any other questions.


Regards,

Bulent
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Data Storage for XAML
Asked by
Dirk
Top achievements
Rank 1
Iron
Answers by
Dirk
Top achievements
Rank 1
Iron
Bulent
Telerik team
Share this question
or