views:

831

answers:

3

I've decided to use Entity Framework for O/R Mapping, and DataAnnotations for validation in my project, and I have now encountered an odd problem when trying to implement this.

This is what I've done:

I have the following entity type

Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null) 
Address Address (not null)
String Phone
String Email

where Name and Address are complex types defined as follows:

Name                                  Address
****                                  *******
String First (not null)               String Street (not null)
String Last (not null)                String ZipCode (not null)
                                      String City (not null)

And the following classes reside in the same namespace as my entities:

public class ContactMetadata
{
    [Required]
    public Name Name { get; set; }
}

[MetadataType(typeof(ContactMetadata))]
partial class Contact { }

However, when I create a new Contact item, the Name and Address types are filled with instances of Name and Address where all values are null, instead of Name and Address having null values themselves. Thus, the Required attribute doesn't throw any errors, although all values are null. How do I work around this?

+1  A: 

So it creates instances of the Name and Address objects who's properties are null? Interesting.

Can you just put the [Required] attribute on the children?

EDIT: I know this might be considered a smelly way of doing this, but for clarity I edit your answer into the post, so that it can easier be found by the next person having problems with this...

Suggested (and accepted, but yet untested) solution:

Write a custom validation attribute which validates against the null values.

blu
Exactly. I created a new class named NameMetadata, using the same structure as above, and set both First and Last names required, but it made no difference.
Tomas Lycken
There's a bounty on this question now - and you're the one closest to get anywhere... ;)
Tomas Lycken
Oh, how sad, I have no idea. Have you maybe tried making a custom attribute and validating against the null value in there?
blu
No, I haven't. But, given that it's done correctly, that will probably work... thanks! =)
Tomas Lycken
A: 

Make sure the names that end up in the HTML fields line up with the property names of the class.

For example, if you have this:

public class Contact {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    [...]
}

Your calls to the HTML helpers should look like this:

<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<%= Html.TextBox("Address.Street") %>
<%= Html.TextBox("Address.City") %>
[...]
Brad Wilson
I haven't gotten to the view part yet - but I will, and when I do this is useful information. Thanks!
Tomas Lycken
A: 

Check out this blog post complex-dataannotations-validation. I think the RequiredAssociation attribute is what you need. You might have to tweak it a little bit for Entity Framework instead of LINQ to SQL.