本文介绍了在Windows Phone 8.1上使用SQLite-我需要一个ORM吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows Phone 8.1应用程序,需要使用数据库,直到有人问我正在使用什么ORM时,这才成为问题.

I am working on a Windows Phone 8.1 app and need to use a database, I didn't that would be an issue until someone asked me what ORM I am using.

所以我下载了&从 http://sqlite.org 安装了SQLite.net库,并将其添加到我的项目中,我还添加了nuget包,这解决了我遇到的错误.

So I downloaded & installed the SQLite.net library from http://sqlite.org and added it to my project, I also added the nuget packages and this resolved the errors I was getting.

现在,我已经创建了到数据库以及基于模型的表的连接.我的问题是,ORM进入哪里?我在做什么我不需要做什么?

Now I have created a connection to my database as well as tables based on my models. My question is, where does the ORM come in? What am I not doing that I need to do?

我的数据库在设计上非常简单,只有两个表.之所以需要它,是因为以后需要使用MS Office API导出数据.

My database is very simple by design, only two tables. The reason I need it is because I will later need to export the data using MS Office APIs.

请帮助.

推荐答案

对象关系映射(ORM)–使您无需编写SQL语句即可从数据库CRUD对象的库. SQLite.NET库是一个非常精简的ORM.看来您做得对,但您可以按照以下分步说明进行检查:

Object Relational Mapping(ORM) – library that allows you CRUD objects from a database without writing SQL statements. The SQLite.NET library is a very lite ORM.It seems that you are doing it right, but you can check it with the following step-by-step instruction:

  1. 向您的解决方案中添加一个新项目,例如YourProject.SQLite,或仅在现有的适当项目之一中创建一个子文件夹.
  2. 通过nuget添加sqlite-net依赖项.
  3. 安装用于Windows Phone的SQLite 扩展.
  4. 现在您可以:
    • 创建连接_connection = new SQLiteAsyncConnection(DatabaseName);
    • 创建数据库等待_connection.CreateTablesAsync(typeof(SyncFile), typeof(TransferFile));
    • CRUD对象await _connection.InsertAsync(entity); _connection.UpdateAsync(entity);
  1. Add a new project to you solution like YourProject.SQLite or just create a sub folder in one of existent appropriate projects.
  2. Add sqlite-net dependency via nuget.
  3. Install SQLite for Windows Phone extension.
  4. Now you can:
    • Create a connection _connection = new SQLiteAsyncConnection(DatabaseName);
    • Create databases await _connection.CreateTablesAsync(typeof(SyncFile), typeof(TransferFile));
    • CRUD objects await _connection.InsertAsync(entity); _connection.UpdateAsync(entity); etc.

我建议您也使用通用存储库模式.它看起来像:

I would recommend you to use generic repository pattern as well. It can look like:

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, new()
{
    private readonly SQLiteAsyncConnection _connection;
    public Repository(SQLiteAsyncConnection connection)
    {
        _connection = connection;
    }
    public async Task<TEntity> GetByIdAsync(object id)
    {
        return await _connection.FindAsync<TEntity>(id);
    }
    public async Task<IReadOnlyCollection<TEntity>> GetAllAsync()
    {
        return await _connection.Table<TEntity>().ToListAsync();
    }
    public async Task<IReadOnlyCollection<TEntity>> GetWhereAsync(Expression<Func<TEntity, bool>> predicate)
    {
        return await _connection.Table<TEntity>().Where(predicate).ToListAsync();
    }
    public async Task AddAsync(TEntity entity)
    {
        await _connection.InsertAsync(entity);
    }
    public async Task UpdateAsync(TEntity entity)
    {
        await _connection.UpdateAsync(entity);
    }
    public async Task DeleteAsync(TEntity entity)
    {
        await _connection.DeleteAsync(entity);
    }
}

这篇关于在Windows Phone 8.1上使用SQLite-我需要一个ORM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:32