1

I'm working on an MVC3 project I have created my database on SQL server 2008 then I add it using entity data model, now I have the .edmx I don't want to generate the code of the classes because till now it's not stable and I don't feel the need to do that except to make the validations I'm wondering if there is a way to make validation directly using the .edmx without generating the code of classes Need server and client side validation Cheers

Yasminette
  • 55
  • 1
  • 11

2 Answers2

6

You can't do it from edmx. You can however have classes that add the metadata to the edmx generated classes.

This adds a metadata containing class to the class that was generated by the edmx:

[MetadataTypeAttribute(typeof(CustomerMetadata))]
public partial class Customer
{
}

and then you have the metadata class which has the metadata attributes:

internal sealed class CustomerMetadata
{
    public int ID;

    [Required]
    [StringLength(60)]
    public string Name;
}

Edit: EF generates all it's classes as partial. That means you can add functionality in a different file to the same class. We use this feature to add an attribute telling .net that there's a class that has the metadata information. In this case the class with the metadata for the Customer class is CustomerMetadata.

It has all the properties that you want decorated with metadata. In this case ID is not actually required to be there.

linkerro
  • 5,318
  • 3
  • 25
  • 29
  • So I have to add a partial class in the model folder with the same name of the table and I make validation on the attributes using the MetadataTypeAttribute. I don't know if I understand well how to proceed – Yasminette Mar 13 '12 at 10:16
0

(I would have added this as a comment but I don't have enough reputation points yet) Be sure that the namespace in your partial class exactly matches the EF-generated class's namespace, including the correct case.

Naughton
  • 124
  • 1
  • 8