tags:

views:

33

answers:

3

Hi,

Say "Foo" is a Linq to SQL entity created in the Linq to SQL designer.

I then have "Bar" which derives from "Foo".

Should I be able to save "Bar" using Linq to SQL (assuming I don't care abut saving any of the extra properties on Bar).

        using (myDataContext ctx = new myDataContext())
        {
            ctx.Foos.InsertOnSubmit(instanceOfBar);
            ctx.SubmitChanges();
        }

Is this supposed to be supported?

Thanks much, Jon

A: 

I'm not sure, but why are you doing it? The entities are all implemented as partial classes, so why don't you just implement what you want in a partial class?

Shlomo
The reason is that I want the Linq to SQL code to be in a library/dll that I can reuse in many applications and in the applications that use this library I would like to extend the entities with application specific knowledge.
Jon Kragh
I would implement a repository pattern then: Don't expose the Linq To SQL Data Context, mark it as internal. Have a thin wrapper over it (repository) then expose that. The repository methods can convert the extended classes to the base entities of the datacontext.
Shlomo
A: 

I've tried to do this once upon a time and couldn't get it to work. Can't remember what the error that was thrown, but to get around it, i basically had to go through all the properties using reflection and copy the properties marked with ColumnAttribute into a new base class instance and then insert that instead. It's not pretty, but it works. I haven't reinvestigated the issue since i implemented it, so if there's a better way, i'd love to know.

Frank Tzanabetis
As far as I know, this is really the only way to do it aside from reimplementing the mapping source. The default implementation of Linq to Sql's AttributeMapping only retrieves the various attributes(TableAttribute, ColumnAttribute, etc) on the base type itself, it ignores anything that's inherited.
rossisdead
It's funny, after I posted this, I just figured it is not supported and went and downloaded automapper http://automapper.codeplex.com/ to map my derived entity to a new instance of the base class. (similar to what you found) Worked great. I also extended my base class with an event that gets triggered before saving so that the derived class serializes its extension data into xml (which is saved into an xml column in the base classes schema).
Jon Kragh
A: 

I'm a stickler for the repository pattern which means that I define my models in an isolated dll (project.Models.dll) and then create a LinqToSql implementation of my IRepository.

The linq classes only exist within the LinqToSql implementation dll and I create extension methods to convert from my models to the linq entities and vice versa.

I've found that this enables you to test more parts of the system without being overly reliant on the database. It is a bit of a pain though, but you only do it once per project.

Which then means that you have full control over the serialization of your objects, and can do pretty much whatever you like with them

Dylan