473,545 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LINQ inheritance problem

Hello everyone,

I have a c# project with a sql server database.
I have a number of lookup tables in my database which I successfully managed
to import into my LINQ dataclasses.

eg.

Table: tlkpColor
(PK) tlkpColorID
strDescription
dtmCreate
strCreateUser

Table: tlkpCompanyType
(PK) tlkpCompanyType ID
strDescription
dtmCreate
strCreateUser

an so on...

I would like to create a generic lookup object to inherit from for my lookup
objects generated from the tables. I have tried to create a generic object
and setting discriminator property to implement inheritance as seen on some
tutorials on the net but I'm just not having any luck (and I have spent a
long time on this).
eg.

Table: tlkpGeneric
strDescription
dtmCreate
strCreateUser

Table: tlkpColor
(PK) tlkpColorID

Table: tlkpCompanyType
(PK) tlkpCompanyType ID

etc...

I've also tried other combinations (eg. overring properties in subclasses)

Essentially, I would like to union all my lookup tables into one generic one
with a composite primary key (id + type/discriminator value) but using
inheritance.

Am I tackling this the right way?

I hope I am making sense, sorry if I am not. Please let me know if I need to
clarify anything.

Thank in advance,

Leo

Jun 27 '08 #1
3 2887
Leo Seccia wrote:
Hello everyone,

I have a c# project with a sql server database.
I have a number of lookup tables in my database which I successfully
managed to import into my LINQ dataclasses.

eg.

Table: tlkpColor
(PK) tlkpColorID
strDescription
dtmCreate
strCreateUser

Table: tlkpCompanyType
(PK) tlkpCompanyType ID
strDescription
dtmCreate
strCreateUser

an so on...
Tip: don't prefix table field names with type descriptions. Also don't
prefix table names with 'tbl' or other prefixes: The entity description
itself should be enough about what it represents.
I would like to create a generic lookup object to inherit from for my
lookup objects generated from the tables. I have tried to create a
generic object and setting discriminator property to implement
inheritance as seen on some tutorials on the net but I'm just not having
any luck (and I have spent a long time on this).
I assume you're talking about Linq to Sql. Linq to Sql only supports
inheritance on 1 table/view, thus the complete inheritance hierarchy is
mapped onto the same table/view.

That aside, I would advice against inheritance for this scenario, as
inheritance should only be used if you are adding specialization fields
to an entity. E.g. you have a base table 'Animal' and you have subtypes
'Dog', 'Fish' etc.

Lookup data is just that: a key and some textdata mostly, mostly
they're constant and there's NO inheritance involved at all,as the
subtypes aren't really extending the supertype.
eg.

Table: tlkpGeneric
strDescription
dtmCreate
strCreateUser

Table: tlkpColor
(PK) tlkpColorID

Table: tlkpCompanyType
(PK) tlkpCompanyType ID

etc...

I've also tried other combinations (eg. overring properties in subclasses)
this requires a table per type, which isn't supported by Linq to Sql.
It is by other o/r mappers though.

However, as I said above, it's not useful here. I understand that you
might think that the fields 'Description', 'CreateUser' and 'Create' are
shared among more than 1 type and therefore could benefit from
inheritance, but you also have to understand that if you have 1 table
per type, performance degrades because you have to join the two tables
together.
Essentially, I would like to union all my lookup tables into one generic
one with a composite primary key (id + type/discriminator value) but
using inheritance.
You shouldn't do this. Don't take this the wrong way, but you're making
a bunch of basic datamodel mistakes: why would you choose to create a
compound PK if 'id' is already unique?

An entity is identified by a unique attribute (field). This can be any
field in the entity itself or by an artificial one added for this
purpose, e.g. 'id'.

The discriminator field has no place in this PK, as it adds NOTHING for
the identification for the entity instance (table row): 'id' is enough.

Please read some basic relational modeling papers. I can recommend this
paper http://www.orm.net/pdf/ORMwhitePaper.pdf and other papers on
http://www.orm.net

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jun 27 '08 #2
Hello Frans,

First of all, I would like to thank you for the reply. Here are a few notes.

"Frans Bouma [C# MVP]" <pe************ ******@xs4all.n lwrote in message
news:Od******** ******@TK2MSFTN GP02.phx.gbl...
Leo Seccia wrote:
>Hello everyone,

I have a c# project with a sql server database.
I have a number of lookup tables in my database which I successfully
managed to import into my LINQ dataclasses.

eg.

Table: tlkpColor
(PK) tlkpColorID
strDescripti on
dtmCreate
strCreateUse r

Table: tlkpCompanyType
(PK) tlkpCompanyType ID
strDescripti on
dtmCreate
strCreateUse r

an so on...

Tip: don't prefix table field names with type descriptions. Also don't
prefix table names with 'tbl' or other prefixes: The entity description
itself should be enough about what it represents.
I know exactly what you mean - I really don't like prefixes and I don't find
them useful.
Unfortunately this part is not up to me and the db already exist.
>I would like to create a generic lookup object to inherit from for my
lookup objects generated from the tables. I have tried to create a
generic object and setting discriminator property to implement
inheritance as seen on some tutorials on the net but I'm just not having
any luck (and I have spent a long time on this).

I assume you're talking about Linq to Sql. Linq to Sql only supports
inheritance on 1 table/view, thus the complete inheritance hierarchy is
mapped onto the same table/view.

That aside, I would advice against inheritance for this scenario, as
inheritance should only be used if you are adding specialization fields to
an entity. E.g. you have a base table 'Animal' and you have subtypes
'Dog', 'Fish' etc.

Lookup data is just that: a key and some textdata mostly, mostly they're
constant and there's NO inheritance involved at all,as the subtypes aren't
really extending the supertype.
I agree with you all the way here, inheritance is definately not going to
help in this scenario and it would be a very bad idea to us it.
>eg.

Table: tlkpGeneric
strDescripti on
dtmCreate
strCreateUse r

Table: tlkpColor
(PK) tlkpColorID

Table: tlkpCompanyType
(PK) tlkpCompanyType ID

etc...

I've also tried other combinations (eg. overring properties in
subclasses)

this requires a table per type, which isn't supported by Linq to Sql. It
is by other o/r mappers though.

However, as I said above, it's not useful here. I understand that you
might think that the fields 'Description', 'CreateUser' and 'Create' are
shared among more than 1 type and therefore could benefit from
inheritance, but you also have to understand that if you have 1 table per
type, performance degrades because you have to join the two tables
together.
>Essentially, I would like to union all my lookup tables into one generic
one with a composite primary key (id + type/discriminator value) but
using inheritance.

You shouldn't do this. Don't take this the wrong way, but you're making a
bunch of basic datamodel mistakes: why would you choose to create a
compound PK if 'id' is already unique?

An entity is identified by a unique attribute (field). This can be any
field in the entity itself or by an artificial one added for this purpose,
e.g. 'id'.

The discriminator field has no place in this PK, as it adds NOTHING for
the identification for the entity instance (table row): 'id' is enough.
The id in the union would not be unique anymore...
Anyway, you are right I shouldn't do this and I am not going to (for obvious
reasons).
I guess it was a really bad way to explain what I want to do.
Basically, I need a way to have a generic class to access all lookup data
access classes in a uniform way and as I am new to LINQ I overlooked at what
inheritance should actually be used for and I explained very badly what I
needed to achieve. Sorry.
Here is the full picture, from an higher level:
We have a very large set of lookup lists that we would like to view, filter,
edit, modify, delete records using one form and common code. Using weakly
typed objects is a bit of a nightmare (eg. from GetTable(Type)) so we
thought, if we have one generic object to cast to and perform linq
operations against, it would be great.
And this is how it all started...
I am sorry if the post appears dumb, but it is our first real linq project
and I'm trying to figure out how to do things.
Please read some basic relational modeling papers. I can recommend this
paper http://www.orm.net/pdf/ORMwhitePaper.pdf and other papers on
http://www.orm.net

FB

--
Thank you for the links, definately interesting...

Regards,

LS
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jun 27 '08 #3
Hello everyone,

I finally solved my problem... All I needed was an interface. ;-)
It works!!!

eg.
var gdi = dc.GetTable(the Type).Cast<IDro pDownItem>();
dropDownItems.D ataSource = gdi.Where(a =a.bObsolete == false);

NB. Using GetTable(type) rather than GetTable<TEntit y>() returns the weakly
typed version... Therefore casting is needed...
"Leo Seccia" <ls*****@msn.co mwrote in message
news:D4******** *************** ***********@mic rosoft.com...
Hello Frans,

First of all, I would like to thank you for the reply. Here are a few
notes.

"Frans Bouma [C# MVP]" <pe************ ******@xs4all.n lwrote in message
news:Od******** ******@TK2MSFTN GP02.phx.gbl...
>Leo Seccia wrote:
>>Hello everyone,

I have a c# project with a sql server database.
I have a number of lookup tables in my database which I successfully
managed to import into my LINQ dataclasses.

eg.

Table: tlkpColor
(PK) tlkpColorID
strDescriptio n
dtmCreate
strCreateUs er

Table: tlkpCompanyType
(PK) tlkpCompanyType ID
strDescriptio n
dtmCreate
strCreateUs er

an so on...

Tip: don't prefix table field names with type descriptions. Also don't
prefix table names with 'tbl' or other prefixes: The entity description
itself should be enough about what it represents.

I know exactly what you mean - I really don't like prefixes and I don't
find them useful.
Unfortunately this part is not up to me and the db already exist.
>>I would like to create a generic lookup object to inherit from for my
lookup objects generated from the tables. I have tried to create a
generic object and setting discriminator property to implement
inheritance as seen on some tutorials on the net but I'm just not having
any luck (and I have spent a long time on this).

I assume you're talking about Linq to Sql. Linq to Sql only supports
inheritance on 1 table/view, thus the complete inheritance hierarchy is
mapped onto the same table/view.

That aside, I would advice against inheritance for this scenario, as
inheritance should only be used if you are adding specialization fields
to an entity. E.g. you have a base table 'Animal' and you have subtypes
'Dog', 'Fish' etc.

Lookup data is just that: a key and some textdata mostly, mostly they're
constant and there's NO inheritance involved at all,as the subtypes
aren't really extending the supertype.

I agree with you all the way here, inheritance is definately not going to
help in this scenario and it would be a very bad idea to us it.
>>eg.

Table: tlkpGeneric
strDescriptio n
dtmCreate
strCreateUs er

Table: tlkpColor
(PK) tlkpColorID

Table: tlkpCompanyType
(PK) tlkpCompanyType ID

etc...

I've also tried other combinations (eg. overring properties in
subclasses)

this requires a table per type, which isn't supported by Linq to Sql. It
is by other o/r mappers though.

However, as I said above, it's not useful here. I understand that you
might think that the fields 'Description', 'CreateUser' and 'Create' are
shared among more than 1 type and therefore could benefit from
inheritance, but you also have to understand that if you have 1 table per
type, performance degrades because you have to join the two tables
together.
>>Essentially , I would like to union all my lookup tables into one generic
one with a composite primary key (id + type/discriminator value) but
using inheritance.

You shouldn't do this. Don't take this the wrong way, but you're making a
bunch of basic datamodel mistakes: why would you choose to create a
compound PK if 'id' is already unique?

An entity is identified by a unique attribute (field). This can be any
field in the entity itself or by an artificial one added for this
purpose, e.g. 'id'.

The discriminator field has no place in this PK, as it adds NOTHING for
the identification for the entity instance (table row): 'id' is enough.

The id in the union would not be unique anymore...
Anyway, you are right I shouldn't do this and I am not going to (for
obvious reasons).
I guess it was a really bad way to explain what I want to do.
Basically, I need a way to have a generic class to access all lookup data
access classes in a uniform way and as I am new to LINQ I overlooked at
what inheritance should actually be used for and I explained very badly
what I needed to achieve. Sorry.
Here is the full picture, from an higher level:
We have a very large set of lookup lists that we would like to view,
filter, edit, modify, delete records using one form and common code. Using
weakly typed objects is a bit of a nightmare (eg. from GetTable(Type)) so
we thought, if we have one generic object to cast to and perform linq
operations against, it would be great.
And this is how it all started...
I am sorry if the post appears dumb, but it is our first real linq project
and I'm trying to figure out how to do things.
>Please read some basic relational modeling papers. I can recommend this
paper http://www.orm.net/pdf/ORMwhitePaper.pdf and other papers on
http://www.orm.net

FB

--

Thank you for the links, definately interesting...

Regards,

LS
>------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jun 27 '08 #4

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

Similar topics

3
1737
by: David Veeneman | last post by:
I've been hearing a lot about LINQ in connection with Orcas, the next release of VS.NET. Micorosoft touts LINQ as the Next Big Breakthrough, but it looks to me like further muddying of application layering. I use an object-oriented development approach, and I don't see how LINQ can be integrated. Am I missing something? Are there any good...
28
16358
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is...
22
10340
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast. public List<ConferenceRoomOldWay(int minimumSeatingCapacity) {
6
1545
by: Dmitry Perets | last post by:
Hello, I am trying to work with MS SQL Server 7 from the release version of Visual Studio 2008 + LINQ to SQL. And the problem is that the LINQ to SQL designer doesn't accept my tables saying that my connection provider is unsupported. Then I found out that LINQ to SQL officially supports only .NET Provider for SQL Server. The problem is...
4
1972
by: Jacek Jurkowski | last post by:
Why is it so slow? I really like that queries but using DataReader i have done my task's much more faster than ising LINQ...
4
2175
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 not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities...
7
11557
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
4
1990
by: JS | last post by:
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...
1
1957
by: Andy B | last post by:
I have to learn how to do one or the other: linq to sql or ado.net entity framework. There will be overhead no matter what way I go since I don't know either one. Which one would be the best one to use overall?
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7918
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7436
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7766
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.