SQL Azure 数据库 - 令人困惑的错误消息 - SQL Server

问题描述

我尝试使用以下代码将数据插入 Azure sql 数据库

Users user = new Users()
                {
                    username = txtUsername.Text,userPassword = txtPassword.Text
                };

await App.client.GetTable<Users>().InsertAsync(user);

当我运行代码时,出现此错误

InvalidOperationExecution:在 Model.Users 类型上找不到 id 成员

所以我检查了我的 C# 模型类:

public class Users
{
        [JsonProperty(PropertyName = "user_id")]
        public int user_id { get; set; }
        public string username { get; set; }
        public string userPassword { get; set; }
}

我尝试将 json 属性user_id 更改为 id,如下所示:

public class Users
{
        [JsonProperty(PropertyName = "id")]
        public int user_id { get; set; }
        public string username { get; set; }
        public string userPassword { get; set; }
}

当我再次运行时,仍然出现错误

InvalidOperationExecution:在 Model.Users 类型上找不到 id 成员

我该怎么办?一开始我已经提供了 user_id 但它显示错误,我哪里出错了?

这是我的数据库

Create table Users
(
    user_id  int primary key identity(1,1),username varchar(30) not null,user     varchar(30) not null,)

解决方法

这个错误其实并没有那么令人困惑。

"InvalidOperationExecution: No id member found on type Model.Users" 表示没有成员是该项目的主键或 ID。

指定密钥的方法有很多种。一种方法是使用 KeyAttribute。

public class Users
{
    [Key]
    public int user_id { get; set; }

收费有用的链接: