开发者

Can a class derived from a Linq to SQL entity still be saved?

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 (开发者_Go百科myDataContext ctx = new myDataContext())
        {
            ctx.Foos.InsertOnSubmit(instanceOfBar);
            ctx.SubmitChanges();
        }

Is this supposed to be supported?

Thanks much, Jon


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.


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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜