具有对象属性的对象不会与SQLite一起保存

时间:2013-09-17 04:43:14

标签: android sqlite c#-4.0 xamarin

我正在使用Xamarin来做一个Android应用程序。我从Web服务获取数据并将其保存到手机上的SQLite数据库中。由于SQLite的限制,我已将ignore添加到对象中的任何不受支持的数据类型中,但我稍后会将它们保存在自己的表中。

物件:

public class Call : BusinessEntityBase
{
    public string Number { get; set; }
    public string WorkOrderNumber { get; set; }
    public DateTime Date { get; set; }
    public DateTime EstimatedStartDate { get; set; }
    public DateTime DueDate { get; set; }
    public DateTime CloseDate { get; set; }
    public string Description { get; set; }
    public string Caller { get; set; }
    public string PONumber { get; set; }
    public string Priority { get; set; }
    public string PriorityRank { get; set; }
    public string CallType { get; set; }
    public string Status { get; set; }
    public string OnHoldCode { get; set; }
    public string DelayCode { get; set; }
    public string SalesRep { get; set; }

    [Ignore]
    public Customer Customer { get; set; }
    [Ignore]
    public Equipment Equipment { get; set; }
    [Ignore]
    public IList<Labor> Labors { get; set; }
}

public class Customer : BusinessEntityBase
{
    public int CallId { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
    public string Phone1 { get; set; }
    public string Phone2 { get; set; }
    public string Email { get; set; }
}

当我保存调用对象时,它保存得很好,我可以稍后检索它就好了。客户对象保存,但不保存任何数据。我得到一个只有Id的空白对象(EntityId继承自BusinessEntityBase)。

DB Helper Code:

    public static int SaveItem<T>(T item) where T : BL.Contracts.IBusinessEntity
    {
        lock (Locker)
        {
            if (item.EntityId != 0)
            {
                Me.Update(item);

                return item.EntityId;
            }

            return Me.Insert(item);
        }
    }

    public static void SaveItems<T>(IEnumerable<T> items) where T : class, BL.Contracts.IBusinessEntity
    {
        lock (Locker)
        {
            try
            {
                Me.BeginTransaction();

                foreach (T item in items)
                {
                    SaveItem(item);
                }

                Me.Commit();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("EXCEPTION OCCURED: " + ex);
                throw;
            }
        }
    }

    public static int DeleteItem<T>(int id) where T : BL.Contracts.IBusinessEntity, new()
    {
        lock (Locker)
        {
            return Me.Delete<T>(new T { EntityId = id });
        }
    }

    public static void ClearTable<T>() where T : BL.Contracts.IBusinessEntity, new()
    {
        lock (Locker)
        {
            Me.Execute(string.Format("delete from \"{0}\"", typeof(T).Name));
        }
    }

    // helper for checking if database has been populated
    public static int CountTable<T>() where T : BL.Contracts.IBusinessEntity, new()
    {
        lock (Locker)
        {
            string sql = string.Format("select count (*) from \"{0}\"", typeof(T).Name);
            var c = Me.CreateCommand(sql, new object[0]);
            return c.ExecuteScalar<int>();
        }
    }

    #endregion

    #region Custom Methods

    public static Call GetCall(string callNumber)
    {
        //lock (Locker)
        //{
        Call call = Me.Table<Call>().FirstOrDefault(x => x.Number == callNumber);

        if (call != null)
        {
            call.Customer = Me.Table<Customer>().FirstOrDefault(x => x.CallId == call.EntityId);// GetCallCustomer(call.EntityId);
        }

        return call;
        //}
    }

这是一层保存:

    public static void SaveCalls(IEnumerable<Call> items)
    {
        try
        {
            IList<Call> list = items as IList<Call> ?? items.ToList();
            DL.DataEngine.SaveItems(list);

            SaveOtherCallData(list);
        }
        catch (Exception ex)
        {
            Debug.WriteLine("EXCEPTION OCCURED: " + ex);

            throw;
        }

    }

    private static void SaveOtherCallData(IEnumerable<Call> items)
    {
        foreach (Call call in items)
        {
            call.Customer.CallId = call.EntityId;
            call.Equipment.CallId = call.EntityId;

            DL.DataEngine.SaveItem(call.Customer);
            DL.DataEngine.SaveItem(call.Equipment);
        }
    }

我不确定是什么导致了我的悲伤。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我弄清楚我的问题是什么。我的一个对象有全局变量而不是属性。