微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐