gpt4 book ai didi

c# - 无法更新,因为没有主键

转载 作者:行者123 更新时间:2023-11-30 12:41:17 25 4
gpt4 key购买 nike

我有一个 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;
}

它破坏了这里的驱动程序代码,正如您所看到的,快速观察显示该字段上没有主键:

enter image description here

这是我最初创建数据库的屏幕截图。

enter image description here

有什么想法吗?

谢谢

最佳答案

希望你一切顺利!

您的数据架构和模型似乎未正确映射。

你可以使用属性来映射。

[Table("Customer")]
class Customer
{
[PrimaryKey, AutoIncrement]
[Column(Name = "CustomerId")]
public int CustomerId { get; set; }

.......
}

问候 MK

关于c# - 无法更新,因为没有主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37960079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com