我有一个
WPF C#桌面应用程序,我正在使用SQLite.
我有这个型号:
using SQLite;
[Table("Customer")]
public class Customer
{
[AutoIncrement]
[PrimaryKey]
public int CustomerId { get; set; }
public string CustomerRef { get; set; }
public string SName { get; set; }
public string FName { get; set; }
public string ContactNo { get; set; }
public string Email { get; set; }
public string Add1 { get; set; }
public string Add2 { get; set; }
public string Add3 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string PCode { get; set; }
public string Country { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrEmpty( Add1))
{
sb.AppendLine(Add1.Trim());
}
if (!string.IsNullOrEmpty(Add2))
{
sb.AppendLine(Add2.Trim());
}
if (!string.IsNullOrEmpty(Add3))
{
sb.AppendLine(Add3.Trim());
}
if (!string.IsNullOrEmpty(Town))
{
sb.AppendLine(Town.Trim());
}
if (!string.IsNullOrEmpty(County))
{
sb.AppendLine(County.Trim());
}
if (!string.IsNullOrEmpty(PCode))
{
sb.AppendLine(PCode.Trim());
}
if (!string.IsNullOrEmpty(Country))
{
sb.AppendLine(Country.Trim());
}
return sb.ToString();
}
}
我添加记录 – 没问题.我尝试更新,我得到一个无法更新,因为没有主键.
这是代码:
var query = DB.Connector.Table<InformedWorkerModel.Customer>().Where(d => d.CustomerRef == customer.CustomerRef).FirstOrDefault();
if (query != null)
{
query.Add1 = customer.Add1;
query.Add2 = customer.Add2;
query.Add3 = customer.Add3;
query.Town = customer.Town;
query.County = customer.County;
query.Country = customer.Country;
query.PCode = customer.PCode;
query.ContactNo = customer.ContactNo;
query.Email = customer.Email;
query.FName = customer.FName;
query.SName = customer.SName;
DB.Connector.Update(query);
return query;
}
它在这里打破了驱动程序代码,正如您所看到的,快速监视显示该字段上没有主键:
这是我最初创建数据库的屏幕截图.
有什么想法吗?
谢谢
最佳答案 希望你做得好!
它看起来像您的数据架构,并且您的模型未正确映射.
你可以使用属性来映射.
[Table("Customer")]
class Customer
{
[PrimaryKey, AutoIncrement]
[Column(Name = "CustomerId")]
public int CustomerId { get; set; }
.......
}
问候MK