Entity Framework Model/Database First Validation with MVC3

asp.net-mvc entity-framework


I want to use MVC 3 and the Entity Framework for my application.

The model will be stored in a different assembly to the MVC app.

The choice I'm making is either to use the EF to generate my entities or to use code first.

With code first, I can decorate members with [Required] etc... But how would I go about adding those attributes if EF has generated entities from the DB?

Having EF generate my entities will save a lot of time, but I want MVC to auto populate the validation depending on how I've decorated my members. Does this make sense? If so, how would I do that?

7/21/2011 11:16:00 AM

Accepted Answer

In that case, MetadataTypeAttribute is used. You can combine it with partial classes to achieve desired results

And by the way, in your place i would do more research when deciding between using Database First and Code First designs. That all is not about saving time when generating entities, there's much more difference between those two approaches. For the time saving purpose, you can use EF Power Tools to generate code first entities from database - simple.

7/21/2011 11:30:17 AM

Popular Answer

Better than auto generating your entities, I recommend you to use Code First or mapping an existing database to POCO's classes (not generating the entities, just creating them by hand and mapping them to the existing database)

Scottgu wrote about using EF "Code First" with an existing database.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow