Question

I am doing this-

File -> new project -> Visual C# .Net Framework 4.5. -> ASP.Net MVC 4 Web Application.

I am creating a class Personne and a class Etudiant deriving from Personne (under the models folder)

Personne.cs

 using System;
 using System.Collections.Generic;  
 using System.Linq;
 using System.Web;

 namespace TestTryHelper.Models
 {
     public class Personne
     {
         public int PersonneId { get; set; }
         public string Nom { get; set;}
         public string Prenom { get; set; }

     }

     public class Etudiant : Personne
     {
         public string Ecole { get; set; }
     }
 }

I am trying to add a HomeController (Controllers/ ADD/ Controller)

Controller Name: HomeController Scalfolding options Template : MVC controller with read/write actions and views using Entity Frameworkd Model Class: Etudiant (TestTryHelper.Models) Data context class:
in the window New Data Context TestTryHelper.Models.TestTryHelperContext click OK Views: Razor
Click ADD

I got this error message

 Unable to retrieve metadata for 'TestTryHelper.Models.Etudiant'.
 One or more validation errrors were detected during mode generation:

 -System.Data.Entity.Edm.EdmEntity,Type:  EntityType 'Etudiant'
 has no key defined.  Define the key for this EntityType.
 -system.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet ' Etudiants' is based on type    
 'Etudiant that has no keys defined.

For some reason it is working for other people. I can fix this by adding a EtudiantId under the Etudiant Class.

Anyone will know why it is working for some people and not for other ?

Thanks

Was it helpful?

Solution

If you change the property PersonneId to just Id I think it will work for both types. Alternatively, decorate the PersonneId property with the [Key] attribute, which will indicate to EF what property you want to use as the table key:

 public class Personne
 {
     [Key]
     public int PersonneId { get; set; }
     public string Nom { get; set;}
     public string Prenom { get; set; }

 }

You may also have to add this to the top of your code:

 using System.ComponentModel.DataAnnotations;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top