我正在使用 Xamarin 做一个 android 应用程序。我正在从 Web 服务获取数据并将其保存到手机上的 SQLite 数据库中。由于 SQLite 的限制,我已将忽略添加到我的对象中任何不支持的数据类型,但我稍后将它们保存在他们自己的表中。
对象:
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 继承的)。
数据库助手代码:
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);
}
}
我不确定是什么导致了我的悲伤。任何帮助表示赞赏。