views:

62

answers:

1

I am investigating some OR/M's and have been unable to find an answer to this question.

I am currently looking at Microsoft's ADO .net entity framework.

Can I override (or use partial classes) to insert custom code into the entities that are created from the database?

It appears that the entities are generated using Xml (not my favourite method of implementation), so I'm not sure if I can put custom code into the classes.

If it can't, can this be done using Linq to SQL?

I have seen T4, and I think it is promising, but at this stage the maintenance moves from entity classes to templates.

+2  A: 

Yes, you can create additional code to existing classes. EF classes are partial. I use it to add validation logic and implement common interfaces. If you want to use DataAnnotations, you have to use additional metadata classes.

To extend your class, just create new class:

public partial class YourEFClassName
{
     //Here you can pute code
}
LukLed
Thanks LukLed, so all I have to do is create a class (say Product) that is also partial with my custom code?
Russell
@Russell: Yes. You just add class with the same name and keyword partial.
LukLed