Hi Experts,
In LINQ to SQL, Can we inherit our entity classes from some other class or interface. I have some common work for all my entities, which I want to code at some common place and call it once.
Regards Parminder
Hi Experts,
In LINQ to SQL, Can we inherit our entity classes from some other class or interface. I have some common work for all my entities, which I want to code at some common place and call it once.
Regards Parminder
Yes, this is possible. Use partial classes. If MyEntity
is your LINQ to SQL entity class, add a partial implementation as follows:
partial class MyEntity : MyBaseClass, IMyInterface {
// do it
}
You can even make your entity class an abstract class.
@Jason's answer is useful for manually tweaking each class. To modify all generated classes to use the same base class there is an attribute in the .dbml file that can be edited manually (no UI for it in VS2008).
Add a EntityBase
xml attribute into the <Database>
xml element, its value is the full name of the base class.
<Database ... EntityBase="MyNamespace.MyBaseClass" ...>
...
</Database>
Its used by the LINQ to Entity Base project.