SQLite数据库中对模型类有什么要求吗? [英] Is there any requirement for model class in SQLite database?

查看:35
本文介绍了SQLite数据库中对模型类有什么要求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ在UWP上编写代码到SQLite.

I am writing code using LINQ to SQLite on UWP.

据我所知,模型是数据库中表行的模板.有关SQLite的文档几乎没有提及模型的限制,也没有提及如何将模型转换为表的行.示例代码通常仅提供具有基本类型的简单案例.

To my understanding, models are templates of rows of the tables in the database. The documentations about SQLite barely mention the restrictions of the models, or how the models are converted into rows of tables. The sample codes usually provide only simple cases with primary types.

我的问题是:

  1. 数据库模型类和普通类之间是否有区别?我至少发现在
  1. Is there any difference between a database model class and a normal class. I find at least in this example(which is a nice one, by the way),

模型类成员装饰有用于指示键的属性.

The model class members are decorated with attributes to indicate keys.

internal class Person
{
    /// <summary>
    /// Gets or sets the identifier.
    /// </summary>
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    ....
}

该属性对于课程来说是必需的吗?还有其他类似的技巧吗?

Is this attribute necessary for the class? Is there any other similar tricks?

  1. 如果模型类和普通类之间存在一般差异,那么SQLite是否需要它或LINQ是否需要它?

  1. If there is any general difference between a model class and a normal class, is it required by SQLite or is it required by LINQ?

复杂数据如何存储在数据库中,例如模型类中的 List<> .属性是否被视为方法或字段,并且属性是否在表中占据一列?

How are complex data stored in database, such as List<> in a model class. Are properties treated as methods or as fields, and do properties take a column in the table?

推荐答案

  1. 唯一的区别是您在数据库上设置了一些属性模型,以便SQLite知道哪个是主键,哪个字段应该被忽略,等等.但是您可以从任何部分重用该类代码.您甚至可以使用数据库和用于数据绑定的UI,但不建议这样做.
  2. LINQ不需要任何属性,它们仅由SQLite使用.
  3. 要存储列表,请使用 SQLite扩展

这是他们网站上的示例:

This is an example from their site:

public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]      // One to many relationship with Valuation
    public List<Valuation> Valuations { get; set; }
}

public class Valuation
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Stock))]     // Specify the foreign key
    public int StockId { get; set; }
    public DateTime Time { get; set; }
    public decimal Price { get; set; }

    [ManyToOne]      // Many to one relationship with Stock
    public Stock Stock { get; set; }
}

这篇关于SQLite数据库中对模型类有什么要求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆