3

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?

2 Answers 2

5

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It can automatically find your primary key if you name the column Id instead of UserId, but personally I never use it, just to let you know
this worked great but now i am getting the following error CREATE DATABASE permission denied in database 'master'.
0

Please make sure you have defined Id as Primary key in your Table and also Make sure you have properly defined properties for your Model Class. Also Please add your proper connection string to config file in addition to DefaultConnection provided by application. I hope this will help.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.