类属性未被包含为sqlite数据库列

33

我有一个实体类,如下:

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

我正在使用SQLite连接类对象DB来创建表格。

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

我想要实现的是,我不希望sqlite在表中为 property3 创建列。有没有办法实现这一点?

我正在使用Windows Store应用程序的SQLiteAsync库。


我认为你不能这样做,因为没有使用列名创建表的方法。现在我们只有一种通过参数传递类名的方法。 - Farhan Ghumra
3个回答

87

您可以使用Ignore属性:

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

你如何定义两个表之间的关系?http://stackoverflow.com/questions/16539683/relational-sqlite-on-windows-rt - hendrix
4
非常感谢大家。现在是2016年,你们的回答一定帮助了我。 - codeshinobi

12

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

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

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

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

Indexed - 应为此属性创建索引。

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

Ignore - 此属性将不会出现在表中。

如果您想了解更多,请查看我更详细的有关这些属性的博客文章:

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


此外,PrimaryKey 内部包含了隐藏的 Indexed 属性,这意味着所有主键默认都已被索引,因此您不需要同时使用这两个属性。 - s3c

0
你可以在代码中添加ignore属性,就像这样:
        [ObservableProperty][property: PrimaryKey, AutoIncrement, Column("ara_id")] public int ara_id;
    [ObservableProperty][property: Ignore] public StareAratare ara_stare;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接