4

ASP.NET MVC2 has strong support for using attributes on entities (validation, and extending Html helper class and more).

If I generated my Model from the Database using VS2010 EF4 Entity Data Model (edmx and it's cs class), And I want to add attributes on some of the entities. what would be the best practice ? how should I cope with updating the model (adding more fields / tables to the database and merging them into the edmx) - will it keep my attributes or generate a new cs file erasing everything ?

(Manual changes to this file may cause unexpected behavior in your application.)

(Manual changes to this file will be overwritten if the code is regenerated.)

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Dani
  • 14,639
  • 11
  • 62
  • 110

3 Answers3

4

Generally you'd create what is called partial classes to extend your auto-generated objects.

Adding Attributes to Generated Classes

xverges
  • 4,608
  • 1
  • 39
  • 60
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • but how do I add an attribute on a field that is already in the entity ? wouldn't it be a redefinition ? – Dani Jan 06 '11 at 20:48
  • thanks, just to make sure: I add a partial empty class (?) and put the [MetadataType(typeof(myclass_ext)) on top of it - and then add another private class in the name of myclass_ext and add the actual fields that I want to change their attributes to that class, and it works on the original ? – Dani Jan 06 '11 at 21:16
  • It looks like I can't accomplish the first task - having an empty partial class. the second I do it, the class looses all it's members. I'm writing it as: public partial class mydb : ObjectContext { [MetadataType(typeof(EventLog_Ext))] public partial class EventLog : EntityObject { }} – Dani Jan 06 '11 at 21:32
1

With the "buddy class" concept, linked above, and data annotations I use this extention method. I forget where I got it, so kudos to the original author.

We use it like

 List<ValidationResult> errorList = new List<ValidationResult>();
        bool bValid = client.IsValid<Client, ClientMetadata>(ref errorList, false);


    public static bool IsValid<T, U>(this T obj, ref List<ValidationResult> errors, bool validateAllProperties = true) where T : IValidatableObject
    {
        //If metadata class type has been passed in that's different from the class to be validated, register the association
        if (typeof(T) != typeof(U))
        {
            TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(T), typeof(U)), typeof(T));
        }

        var validationContext = new ValidationContext(obj, null, null);
        var validationResults = new List<ValidationResult>();
        Validator.TryValidateObject(obj, validationContext, validationResults, validateAllProperties);

        errors = validationResults;

        if (validationResults.Count > 0)
            return false;
        else
            return true;
    }
William
  • 1,375
  • 12
  • 27
0

We use partial classes, but if you need them persisted and handled by EF, the "Update Model from Database" option is your best friend.

lukiffer
  • 11,025
  • 8
  • 46
  • 70