Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When adding controller unable to get metadata mvc4

I am trying to create my first mvc class and am having an issue getting started. I am using a shared database on my hosting account and have created the following class

namespace MvcApplication2.Models
{
    public class tUsers
    {

            public int UserID { get; set; }
            public string Username { get; set; }
            public string UserEmail { get; set; }
            public string UserPassword { get; set; }
            public int GroupId { get; set; }


    }

    public class tUsersDBContext : DbContext
    {
        public DbSet<tUsers> tUsers { get; set; }
    }
}

and here is my connection string

   <add name="DefaultConnection" connectionString="Data Source=gdfgdfgdfg;Initial Catalog=fsdf_fdsf;Persist Security Info=True;User ID=ddd_reddntal;Password=fgfggfdg" providerName="System.Data.SqlClient" />

the error i get is

Unable to retrieve metadata for 'mvcapplication2.models.tusers' one or more validation errors were detected during model generation:

Entity type users has no defended key.

Also do my model names need to match my table names exact?

like image 609
HELP_ME Avatar asked Dec 05 '25 10:12

HELP_ME


1 Answers

Entity Framework can not identify the primary key property of your tUsers class. Annotate UserID property with Key attribute. If your table name is different from the class name use the Table attribute to specify the table name.

Try to use proper naming conventions for class names. In this case it should be User not tUsers.

[Table("MyTableName")]
public class tUsers
{       
        [Key]
        public int UserID { get; set; }
        public string Username { get; set; }
        public string UserEmail { get; set; }
        public string UserPassword { get; set; }
        public int GroupId { get; set; }
}
like image 87
Eranga Avatar answered Dec 07 '25 22:12

Eranga