简体   繁体   中英

How to store data in a database in Windows Phone Runtime?

I give up now after 3 days of searching!

I want to store data in a database in my Windows Phone Runtime app, but couldn't find a proper database solution.

I checked SQLite-net but it doesn't support a simple relationship between classes. So I couldn't define a list of items in my classes.

Then I installed SQLite-net Extensions as a solution to that problem. but it doesn't have a proper package for Windows Phone Runtime . It needs a ISQLitePlatform implementation as a parameter in SQLiteConnection() which only god knows where to find the right dll.

So my question is, all these apps available, what database they use? how can I store my data in a DataBase?

Edit

It seems they fixed the issue a couple of hours ago. See: https://bitbucket.org/twincoders/sqlite-net-extensions/issue/27/error-while-adding-sqlite-net-extension-to

Old Post

Yes. You're right. This is not included by default and you will have to do many things on your own. But implementing relationships is not that hard:

// Query entity A
var AList = App.DB.Table<EntityA>().ToList();
if (AList == null || AList.Count == 0)
    return;
// Get entity B for each item
foreach (EntityA ea in AList)
{
    // I'm retreiving just a single element here.. of course you can easily pull a list here for one to many relationships..
    var eb = App.DB.Table<EntityB>().Where(b => b.EntityAIndex == ea.DBId).FirstOrDefault();
    if (eb == null)
        continue;
    ea.EntityB = eb;
}

But SQLite is the way to go here and unless you're not working with millions of records pulling a simple relationship from your DB won't take too much time (I have 3rd degree relationships and they are retrieved instantly). All you have to do is setting the foreign key manually when you add objects to your DB. But that's a single line of code.

entityA.entityBId = entityB.DBId;

Also writing the code to read entities with relationships is not that much of a hassle. Sure, that's nothing you would want to write manually but it's just a few extra lines.

Btw: I am using sqlite-net.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM