472,951 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Linq to SQL: insert subclassed row

JS
Using Linq to work with an SQL database has worked out well so far,
however I had a problem recently where I wanted to subclass one of the
auto-generated table classes. I'd like to be able to insert an
instance of the subclass into the table, but an exception is thrown.

I'm not at my dev computer right now, so I don't have the exception
details but I wondered if anyone knew why we can't do this or what I
need to do to insert an instance of a subclass.

Thanks.
Sep 9 '08 #1
4 1973
Does the data-context know about the subclass? i.e. via a
discriminator? LINQ-to-SQL is very specific about types; you can't
arbitrarily use your own subclasses if the data-context doesn't expect
it - it wouldn't be able to re-create it, for instance, and thus there
would be identity management issues if you added such an item and then
searched for it via a query.

Marc
Sep 9 '08 #2
JS
Does the data-context know about the subclass? i.e. via a
discriminator?
No, other than the fact that my class is effectively an instance of
the base class which the data context does know about. And I'll have
to look up what a discriminator is so I can't answer the 2nd part of
your question.

I don't quite understand why Linq to SQL does not allow this. If I
wanted to query for my item, I would expect an instance of the base
class to be returned.

Luckily my current application is not performance critical in this
part of the app, so I can clone my subclass instance as the base class
and add it to the db.
Sep 11 '08 #3
Basically, LINQ-to-SQL also allows inheritance, and allows (for example)
the class of a type to map to a column - the "discriminator". So if you
have a table:

Customers
ID Type ....etc...
1 R ....
2 S ....
3 R ....

you can map your customers with "Type=S" to your SuperCustomer type, and
leave your regular customers "Type=R" mapped to your Cutomer type. The
data-context knows how to handle this for both querying and saving data.

This means that when a data-context is given an object, it looks at the
type to understand how to handle the record.

Another perspective here is that the whole point of an ORM is that if
you put something in, you can get it back again. In your scenario, if
you give it a subclass instance that it isn't expecting, then the
data-context *knows* that it can't possibly ever give it you back (in
the same form), so it is (quite reasonably) refusing to handle that object.

Marc
Sep 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: news.microsoft.com | last post by:
Hello people: First of all forgiveness for my English. I am working with Linq to SQL, Visual Studio 2008 and SQL Server 2000. When you delete a record and then go back to Insert (ie, insert a...
2
by: news.microsoft.com | last post by:
Hello people: First of all forgiveness for my English. I am working with Linq to SQL, Visual Studio 2008 and SQL Server 2000. When you delete a record and then go back to Insert (ie, insert a...
2
by: Andy B | last post by:
Hi... I have an sql server 2005 database that has a table called TestXml in it. The table has the columns ID (identity, int) and MyXml(xml). I created a stored proc on the server to insert a row...
0
by: Lloyd Sheen | last post by:
I have an existing set of tables which I have put into a Linq to SQL class. Those tables I have had no problems with. I have created some new tables and when I add them to the a new Linq to SQL...
15
by: shapper | last post by:
Hello, I have two Lists: A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null, Peter) } B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) } I want to...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: bob laughland | last post by:
Hi All, I am using a combination of LINQ to SQL and bulk insert. In the process of performing 'one unit of work' I will be doing things like reading, and deleting records using LINQ to SQL and...
3
by: bob laughland | last post by:
Hi All, I am using a combination of LINQ to SQL and bulk insert. In the process of performing 'one unit of work' I will be doing things like reading, and deleting records using LINQ to SQL and...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.