views:

98

answers:

2

I have a class Address Generated by entity Framework. I Have an propertie AddressID in this class.

I Would like to be able to add some treatement for this prop in the set process. EX :

  public partial class Address
  {
       public bool _AddressID;
       public bool AddressID{get return AddressID;}
       set{
            if(value == -1) _AddressID = null;
       }
   }

Thanks

A: 

Code First in EF4 is an option - it allows you to fully control all of the code. However, another option is to customize the EF4 T4 templates that ship with EF4. If you have certain patterns in your code that you consistently use, this would be a good approach. You can read more about how to customize the templates here: Customizing Entity Classes in VS2010

Steve Michelotti
+2  A: 

Of course you can't redefine your AddressID in order to put your custom logic in its setter, as you'll get compiler error:
The type Address already contains a definition for 'AddressID'

But no worries, if you take a look at the EF generated code for your EntityObject (let's assume its name is Address) you'll see that every scalar property of generated Address class has its own version of OnPropertyChanging and OnPropertyChanged method. For example, OnAddressIDChanging and OnAddressIDChanged in this case.

As you can see below, there is no default implementation for these two methods, only a declaration. This perfectly provides you the opportunity to execute custom logic as the property is about to change (PropertyChanging) as well as just after the property value has changed (PropertyChanged).

// From the designer code for Address class:
partial void OnAddressIDChanging(global::System.Int32 value);
partial void OnAddressIDChanged();


This is how your Entity Model designer code already is look like (hypotetically):

public global::System.Int32 AddressID {
    get {
        return _AddressID;
    }
    set {
        if (_AddressID != value) {
            // OnPropertyChanging method get called here:
            OnAddressIDChanging(value);

            ReportPropertyChanging("AddressID");
            _AddressID = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("AddressID");

            // OnPropertyChanged get called here:
            OnAddressIDChanged();
        }
    }
}


So all you need to do in order to hook up your custom code is:

public partial class Address {
    partial void OnAddressIDChanged() {
        if(AddressID == -1) {            
            AddressID = 0;
        }
    }       
}



By the way, about other posted answers - with all due respect to them - if you want this solution for a production application then you cannot use "Code First" since it merely is a CTP as for now and will be part of the next release for EF, so it cannot be an option.
About customizing default code generation, while this is indeed possible since in VS 2010, Entity Framework itself also uses T4 for designer code generation and we can take advantage of it by changing the T4, But it is an option only if you want to fundamentally change how the entity classes are generated in general and you cannot use it for customizing a setter logic for a specific entity.

Morteza Manavi
Your Solution likes Very very good. But when I try it, IIS stop running and my webpage stop to response. Do you know why .
Jean-Francois
Sorry, I forgot to add a condition in the partial function.
Jean-Francois
You are welcome. I am glad it helped. BTW, put a breakpoint in your custom OnAddressIDChanged method and see how it get called. Let me know if you have any further questions :)
Morteza Manavi