9

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?

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, When I do that I get Ambiguous name error. Here is what I've got in my partial class (the entity is called Item in the model):
[MetadataType(typeof(Item))] public partial class Item { [StringLength(10), Required] public string MaterialName { get; set; } }
paul, the ambiguity 'could' be to do with the pre-existing edmx ef file maybe??
Generally, naming convention for metadata classes is [className]Metadata. For Item entity, good naming for its metadata would be ItemMetadata. Try this to avoid ambiguous name conflicts
but that approach makes necessary to edit the class generated by EF, whihc is not good because the changes would be overwritten if the class is re-generated..
|
2

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.

Comments

1

Check this out: In your model template (file with extension model.tt) you can hack this template for generating decorators, in this example I add the [Required] decorator plus an error message

var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
    foreach (var edmProperty in simpleProperties)
    {
        if(!edmProperty.Nullable)
        {#>
[Required(ErrorMessage="<#=String.Format("The field {0} is required",edmProperty.ToString())#>")]<#
        }#>
<#=codeStringGenerator.Property(edmProperty)#><#
    }
}

So the result is something like this

    [Required(ErrorMessage="The field Id is required")]
    public long Id { get; set; }

PS: You can also add the using System.ComponentModel.DataAnnotations; by editing the template.

Hope this can help you up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.