I implement an ASP.NET MVC (.NET Framework) identity, and the Registration Page and Login Authentication page were as expected auto-generated.
I would like to have a separate page though for List of REGISTERED USERS and this is what I have done.
Upon doing, I got this error > No Key is defined in the View Model
So I tried to add the Id Property in the model like this
public class RegisterViewModel
{
[Key]
public string Id { get; set; }
....
}
But still get the same error.
Please advise
A primary key should uniquely identify a row. It is uncommon to use a string as a primary key.
If it indeed makes sense and the string IS the only unique identifier for your table, then you must let Entity Framework know that your key is not an identity column, because it by default tries to apply this property to the [Key] column.
read more on IDENTITY in T-SQL
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-ver15
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string FooId { get; set; }
All that being said, you should probably go with an integer
or Guid
您需要将您的属性 ID 从字符串更改为整数
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.