Is there an automatic way to add base classes to Linq2Sql entities?
I know I can define a partial and implement that but there has to be an automated way, right?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The LINQ-to-SQL code-generator supports this directly.
The base-class for the data-context can be set in the designer, as the
Base Classproperty. Alternatively, edit the dbml directly: right click, ‘Edit With…’, ‘XML Editor’To change the base class for the entities, set the type:
To change the base class for the data-context, set the type:
In both cases, use the fully-qualified type in the attribute. Et voila.
Note that because it is very literal, you can also use this approach to make your entities implement an interface – for example, when my classes have properties like
LastUpdatedandUpdatedBy, I might have anIAuditableinterface that defines these. Then I can put code in my data-context’sSubmitChanges(override) that callsGetChangeSet()and sets these values for all theIAuditableentities being updated; very sweet.