如何将C#对象映射到SQLite数据库表?

时间:2018-01-15 09:58:14

标签: c# sqlite object mapping

我有以下SQLite表

enter image description here

我有以下C#类:

[XmlRoot(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")]
public class Article
{
    [XmlElement(ElementName = "id", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string Id { get; set; }
    [XmlElement(ElementName = "name", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string Name { get; set; }
    [XmlElement(ElementName = "quantity", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string Quantity { get; set; }
    [XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string SellPrice { get; set; }
    [XmlElement(ElementName = "sellid", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string SellId { get; set; }
    [XmlElement(ElementName = "whole_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string WholePrice { get; set; }
    [XmlElement(ElementName = "group", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string Group { get; set; }
    [XmlElement(ElementName = "barcode", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string Barcode { get; set; }
    [XmlElement(ElementName = "measure", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public string Measure { get; set; }
}

[XmlRoot(ElementName = "DataSet1", Namespace = "http://tempuri.org/DataSet1.xsd")]
public class Articles
{
    [XmlElement(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")]
    public List<Article> AllArticles { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
}

我得到这样的文章列表:

XmlSerializer deserializer = new XmlSerializer(typeof(Articles));
TextReader textReader = new StreamReader(xmlFilePath);
Articles articles = new Articles();
articles = (Articles)deserializer.Deserialize(textReader);
textReader.Close();

但是,当我尝试在表articles中插入文章列表时:

using (var connection = new SQLiteConnection(System.IO.Path.Combine(apkFolder, "MobileSell.db")))
{
    connection.InsertAll(articles.AllArticles);
}

抛出异常:

SQLite.SQLiteException: no such table: Article at SQLite.SQLite3.Prepare2

我的问题是如何让程序理解Article对象必须是表articles中的单行

1 个答案:

答案 0 :(得分:1)

你读过SQLite规范吗?

SQLite具有[Column][Table]属性等属性。你应该使用那些。像这样:

[Table("ContactInfo")] // SQLite attribute
[DataContract(Name = "ContactInfo", Namespace = "")]
public sealed class ContactInfo : DatabaseTableBase
{
    /// <summary>
    /// Unique Id of the codebook.
    /// </summary>
    [PrimaryKey] // SQLite attribute
    [AutoIncrement] // SQLite attribute
    [Column("UniqueId")] // SQLite attribute
    [IgnoreDataMember]
    public override long UniqueId { get; set; }

    [Column("ClaimId")] // SQLite attribute
    [NotNull] // SQLite attribute
    public long ClaimId { get; set; }

    [Column("ContactType")] // SQLite attribute
    [NotNull] // SQLite attribute
    public ContactTypes ContactType { get; set; }

    public ContactInfo()
    {
    }
}

在您的情况下,您将使用类Article

的属性进行注释

希望它有所帮助。