Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: An exception of type 'System.Data.Entity.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Marko Ma:

I'm studying the book of 'Pro MVC5'. I'm following the example in this book. The error happens on Binding Data from Mock to localDb which is the example of Chapter 7.4.5 the crash screenshot is here. Enter image Description here scroll down enter image description here and the detail is '{"One or more validation errors were detected during model generation:\r\n\r\nSportsStore.Domain.Concrete.Product: : EntityType 'Product' has no key defined. Define the key for this EntityType.\r\nProducts: EntityType: EntitySet 'Products' is based on type 'Product' that has no keys defined.\r\n"}'

private void AddBindings()
//Mock mock = new Mock();
//mock.Setup(m => m.Products).Returns(new List{
// new Product { Name = "Football", Price = 25 },
// new Product { Name = "Surf board", Price = 179 },
// new Product { Name = "Running shoes", Price = 95 }
// });

The code which I commented works well while the uncommented code will lead to crash. Crash triggered on List.cshtml. the line foreach(var p in Model) I know it could be the localDb's problem but I don't know how to debug it and fix it. The connectingString in Web.config is as following.

localDb screenshot. enter image description here

VS DB settings screenshots enter image description here


public class Product
public int ProjecID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }

the Stack Trace enter image description here

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: An exception of type 'System.Data.Entity.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription