【问题标题】:SQL Azure database - confusing error message - SQL ServerSQL Azure 数据库 - 令人困惑的错误消息 - SQL Server
【发布时间】:2021-04-07 13:43:15
【问题描述】:

我尝试使用以下代码将数据插入 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,
)

【问题讨论】:

标签: c# azure xamarin azure-sql-database


【解决方案1】:

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

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

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

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

收费有用的链接:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2011-04-23
    • 1970-01-01
    • 2014-10-11
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多