none
Entity Framework Code-First Aproach and ModelValidationException RRS feed

  • Question

  • I'm building an MVC application and trying to use code-first aproach while building entities and database.

    I get ModelValidationException when I run code. (Message: System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code)

    This is code:

    Controller:

    public class HomeController : Controller { PhonesDB _db = new PhonesDB(); public ActionResult Index() { var model = from b in _db.ProductList //Exception appears here select b; return View(model); } }


    Models:

    namespace SmartPhoneCatalog.Models {
        public class Products {
            public int ProductID { get; set; }
            public string Manufacturer { get; set; }
            public string Name { get; set; }
            //...
        }
    }

    namespace SmartPhoneCatalog.Models {
        public class PhonesDB:DbContext {
            public DbSet<Products> ProductList { get; set; }
        }
    }

    and this is the view:

    @model IEnumerable<SmartPhoneCatalog.Models.Products>
    
    @{
        ViewBag.Title = "Home";
    }
    
    @foreach (var item in Model) {
        <div>
            <h4>@item.Name</h4>
            <div>@item.Price</div>
            <hr/>
        </div>
    }

    Am I missing something?

    Tried starting empty mvc application and then template but had the same problem on both one.

    Any ideas how to fix it?


    • Edited by mishonacro Friday, August 29, 2014 5:29 PM spelling mistake
    • Moved by KareninstructorMVP Friday, August 29, 2014 6:24 PM Move from C# for dedicated support with EF
    Friday, August 29, 2014 5:21 PM

Answers

  • Well, I don't use the code first approach with MVC solutions. I use the database first approach with a DAL (Data Access Layer), with DTO(s) and the DTO(s) are used at the View model. The EF entities never come past the DAL, N-tier MVC solutions with separation of concerns. No database crap up at the Controller. I know that's what they teach is using EF up at the MVC controller and code first. But I learned to abandon that long ago and use a N-tier approach with MVC solutions and EF. A View Model is using the DTO(s).

    Anyway, the MVC section is at the link below, and there is also the EF forum too. They can help you.

    http://forums.asp.net/

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    • Marked as answer by mishonacro Friday, August 29, 2014 7:11 PM
    Friday, August 29, 2014 6:27 PM

All replies

  • Well, I don't use the code first approach with MVC solutions. I use the database first approach with a DAL (Data Access Layer), with DTO(s) and the DTO(s) are used at the View model. The EF entities never come past the DAL, N-tier MVC solutions with separation of concerns. No database crap up at the Controller. I know that's what they teach is using EF up at the MVC controller and code first. But I learned to abandon that long ago and use a N-tier approach with MVC solutions and EF. A View Model is using the DTO(s).

    Anyway, the MVC section is at the link below, and there is also the EF forum too. They can help you.

    http://forums.asp.net/

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    • Marked as answer by mishonacro Friday, August 29, 2014 7:11 PM
    Friday, August 29, 2014 6:27 PM
  • Found a solution.

    [Key] was missing in Products :(

    Friday, August 29, 2014 9:04 PM