加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:32:32 所属栏目:百科 来源:网络整理
导读:我有一个 WPF C#桌面应用程序,我正在使用SQLite. 我有这个型号: using SQLite;[Table("Customer")]public class Customer{ [AutoIncrement] [PrimaryKey] public int CustomerId { get; set; } public string CustomerRef { get; set; } public string SNam
我有一个 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读