【问题标题】:Class Property Not be included as sqlite database column类属性不包含在 sqlite 数据库列中
【发布时间】:2013-04-30 04:26:07
【问题描述】:

我有一个实体类

  public class someclass
  {
      public string property1 {get; set;}
      public string property2 {get; set;}
      public string property3 {get; set;}
  }

并使用 sqlite 连接类 obj DB 我正在创建表

  Db.CreateTableAsync<someclass>().GetAwaiter().GetResult();

我想要实现的是,我不希望 sqlite 在表中为property3 创建列。有什么方法可以实现吗?

我正在为 Windows 商店应用程序使用 SQLiteAsync 库。

【问题讨论】:

  • 我认为你不能,因为没有使用列名创建表的方法。现在我们只有一个将类名作为参数传递的方法。

标签: c# sqlite windows-runtime windows-store-apps sqlite-net


【解决方案1】:

你可以使用Ignore属性:

public class someclass
{
    public string property1 { get; set; }
    public string property2 { get; set; }
    [Ignore]
    public string property3 { get; set; }
}

【讨论】:

【解决方案2】:

正如 chue x 之前所说,您应该使用 Ignore 属性,但我想我会提供更多关于所有属性的作用的信息,因为这似乎是一些在此线程中有用的信息。

以下是可供使用的属性类型的快速摘要(适用于那些不喜欢阅读而只想快速了解的人):

PrimaryKey - 此属性是表的主键。仅支持单列主键。

AutoIncrement - 该属性由数据库在插入时自动生成。

Indexed - 该属性应该有一个为其创建的索引。

MaxLength - 如果此属性是字符串,则 MaxLength 用于指定 varchar 的最大大小。默认最大长度为 140。

忽略 - 此属性不会出现在表格中。

如果您想了解更多信息,请查看我关于这些属性的更深入的博文:

http://lukealderton.com/blog/posts/2016/august/sqlite-attributes-and-what-they-do.aspx

【讨论】:

  • 另外PrimaryKey里面隐藏了Indexed,这意味着默认情况下所有主键都被索引,你不需要同时使用这两个属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多