473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1985
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.