Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am working in MVC 4 application and using Entity Frame work to connect with Sql Server data base. I have created a View with the name of vUserRole in my Sql db.

I have class defined to map data from the view as follows

public class vUserRole
   {
       public int UserId { get; set; }

       public string LoginId { get; set; }

       public string UserName { get; set; }

       public int RoleId { get; set; }

       public string RoleName { get; set; }
   }


and I have defined the property in my DBContext class as follows

public DbSet<vUserRole> vUserRoles { get; set; }


But when i run the project I get the following exception

{"One or more validation errors were detected during model generation:\r\n\r\n\\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'vUserRole' has no key defined. Define the key for this EntityType.\r\n\\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'vUserRoles' is based on type 'vUserRole' that has no keys defined.\r\n"}


It is asking for a Key to be defined for the class vUserRole. But it is representing a view.


Please, help me.

What I have tried:

I need help. Thanks in advance
Posted
Updated 13-Mar-17 1:27am
v2
Comments
F-ES Sitecore 13-Mar-17 5:00am    
Google "EntityType has no key defined"

1 solution

You may need to add key attribute. Please try.

For Ex:

  [Key]
public int UserId { get; set; }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900