C# 无法更新,因为没有主键
我有一个WPF C#桌面应用程序,我正在使用SQLite 我有这个模型:C# 无法更新,因为没有主键,c#,wpf,sqlite,C#,Wpf,Sqlite,我有一个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
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;
}
var query=DB.Connector.Table().Where(d=>d.CustomerRef==customer.CustomerRef.FirstOrDefault();
if(查询!=null)
{
query.Add1=customer.Add1;
query.Add2=customer.Add2;
query.Add3=customer.Add3;
query.Town=customer.Town;
query.country=customer.country;
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(查询);
返回查询;
}
它会在此处中断驾驶员代码,正如您所看到的,快速手表在字段上没有显示主键:
这是我最初创建数据库的屏幕截图
有什么想法吗
谢谢希望你做得很好 看起来您的数据模式和模型没有正确映射 可以使用属性来映射
[Table("Customer")]
class Customer
{
[PrimaryKey, AutoIncrement]
[Column(Name = "CustomerId")]
public int CustomerId { get; set; }
.......
}
关于MK@marc_s是的,我知道,但是我的代码和数据模式显示PK已经设置好了谢谢,我在你发布之前尝试过了,但仍然得到了相同的错误:(好的,我刚刚检查了代码,有一个函数GetMapping(objtype)。你确定它工作正常吗?嗨,这就是我在问题中所说的:)这就是它坠落的地方。这段代码是从SQLLiteNuGet下载的。我现在正在寻找一个不同的驱动程序EAH,那么最好使用不同的驱动程序进行检查。Sqllite net将是一个不错的选择:)谢谢您的反馈。换司机。所有工作现在:)+1的建议