MvcMusicStore lesson 4 - Runtime errors

Jun 10, 2012 at 9:37 PM
Edited Jun 10, 2012 at 9:58 PM

After fixing the SampleData class  more errors occur with entities

At runtime the following errors occur

One or more validation errors were detected during model generation:

System.Data.Edm.EdmEntityType: : EntityType 'Genre' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Genres� is based on type �Genre� that has no keys defined.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

System.Data.Edm.EdmEntityType: : EntityType 'Genre' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Genres� is based on type �Genre� that has no keys defined.


Source Error:

Line 35:     //    new Genre { Name = "Rock"} 
Line 36:     //};
Line 37:             var genres = storeDB.Genres.ToList();
Line 38:             return View(genres); 
Line 39: 


Source File: D:\mvc\MvcMusicStoreDemo\MvcMusicStore\MvcMusicStore\Controllers\StoreController.cs Line: 37

Stack Trace:

[ModelValidationException: One or more validation errors were detected during model generation:

	System.Data.Edm.EdmEntityType: : EntityType 'Genre' has no key defined. Define the key for this EntityType.
	System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Genres� is based on type �Genre� that has no keys defined.
]
   System.Data.Entity.ModelConfiguration
Coordinator
Jun 12, 2012 at 8:15 AM

Can you make sure that your Genre class has a GenreId that's defined exactly like this?

public int      GenreId     { get; set; }

Note that spelling is important, since Entity Framework Code First uses conventions to find the key based on the class name and properties.