tags:

views:

376

answers:

3

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?

A: 

EDIT: Marc's answer is clearly the right way to go in this case, but I'm leaving this answer here for posterity - it's a handy trick to know about in some cases, where you only need a partial class to change the inheritance/interfaces of a class.

I don't know of any automated way, but if you wanted to do this for multiple classes and only wanted to specify the base class, you might consider creating a file just for the partial class declarations. I use this approach in Protocol Buffers to make the internal representation classes which are autogenerated implement a particular interface.

See PartialClasses.cs for this example as code. (Ignore the TODO - it's fixed locally, but I haven't pushed for a little while :)

Jon Skeet
"I know I can define a partial and implement that but there has to be an automated way, right?" -1, sorry Skeet, but that wasn't helpful
jfar
So if there *is* no automated way, are you going to downvote everyone, even if they're still trying to make your code more manageable? Yes, you knew about creating your own partial class - but you may not have thought of putting them all in one file to avoid a source file explosion.
Jon Skeet
Not necessarily, your answer is particularly bad because the only additional info you've provided is "you can put multiple classes in a single file".
jfar
And why is that additional info "particularly bad"? It's a valid way of making the code more manageable IMO - it would be easy to write something to automatically generate such a file if you really wanted to, and if not it's trivial to keep up to date anyway. Much simpler than multiple tiny files.
Jon Skeet
(Having said all this, the template approach suggested by Todd is great *if* you don't want to also use DBML. I'm just pointing out an alternative to make your life easier if the only thing you want to change is how you specify a base class.)
Jon Skeet
+1  A: 

Some people have written their own code templates in place of the DBML approach. Going that route you can easily add the base classes yourself.

LINQ to SQL template for Visual Studio 2008 is a good starting point

Todd Smith
+5  A: 

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 Class property. Alternatively, edit the dbml directly: right click, "Edit With...", "XML Editor"

To change the base class for the entities, set the type:

<Database EntityBase="Some.NameSpace.Foo" ... >

To change the base class for the data-context, set the type:

<Database BaseType="Some.NameSpace.Bar" ... >

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 LastUpdated and UpdatedBy, I might have an IAuditable interface that defines these. Then I can put code in my data-context's SubmitChanges (override) that calls GetChangeSet() and sets these values for all the IAuditable entities being updated; very sweet.

Marc Gravell