Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Validation with Entity Framework Model/Database First

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?

like image 773
Paul Avatar asked Jul 21 '11 11:07

Paul


2 Answers

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.

like image 137
archil Avatar answered Oct 20 '22 03:10

archil


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.

like image 29
Jesus Rodriguez Avatar answered Oct 20 '22 03:10

Jesus Rodriguez