Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:38:47+00:00 2026-06-09T13:38:47+00:00

I am new to ASP.net MVC. I am trying to create a viewmodel to

  • 0

I am new to ASP.net MVC. I am trying to create a viewmodel to display a join of data. Here is some example code:

   public class Person
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public ICollection<Relative> Relatives { get; set; }

}

public class Relative
{
    [Key]
    public int ID {get; set; }
    public Person Person { get; set; }
    public RelationType RelationType { get; set; }
}

public class RelationType
{
    [Key]
    public int ID { get; set; }
    public string Description { get; set; }
}

public class PersonViewModel
{
    public string Name { get; set; }
    public ICollection<string> RelativeNames { get; set; }
    public ICollection<string> RelativeTypes { get; set; }
}

public class PersonContext : DbContext
{
    public DbSet<PersonViewModel> people { get; set; }
}

When I try to create my controller through Visual Studio, I get the following error:

Unable to retrieve metadata for PersonViewModel. One or more validations errors were detected during generation:
EntityType ‘PersonViewModel’ has no key defined. Define the key for this EntityType.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:38:48+00:00Added an answer on June 9, 2026 at 1:38 pm

    The error is self explanatory. You need to add an Id field to the PersonViewModel which will have to be decorated with [Key] as you have rightly done in the classes above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to ASP.NET-MVC and I am trying to create a simple blog
I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into
I'm trying to use the geocoding code from here in my ASP.NET MVC 2
I'm new to ASP.NET MVC and I'm trying to create a very simple blog
I am new to ASP.NET MVC. I am trying to create what I thought
I've been learning asp.net mvc 3. So, basically I'm trying to create a new
I'm new to ASP.NET MVC 3 and I'm trying to create a edit view
I am new to ASP.net MVC. I am trying to save the data into
I am using ASP.NET MVC . I am trying to create 2 new records
I am new to ASP.NET MVC 3. I am trying to create a basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.