The Model class should be changed to :
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace MvcApplication1.Models
{
[Table("studentdetails")]
public class student
{
[Key]
public int RollNo { get; set; }
public string Name { get; set; }
public string Stream { get; set; }
public string Div { get; set; }
}
}
Make sure public members of student class are defined as properties w/ {get; set;}
(yours are public variables - a common mistake).
Place a [Key]
annotation on top of your chosen property.
There are several reasons this can happen. Some of these I found here, others I discovered on my own.
Id
, you need to add the [Key]
attribute to it.public
uint
, ulong
etc. are not allowed.i know that this post is late but this solution helped me:
[Key]
[Column(Order = 0)]
public int RoleId { get; set; }
added [Column(Order = 0)] after [Key] can be added by increment by 1:
[Key]
[Column(Order = 1)]
public int RoleIdName { get; set; }
etc...
In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".
I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.
Using the [key] didn't work for me but using an id property does it. I just add this property in my class.
public int id {get; set;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With