Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Linq2Sql limitation with abstract base classes

Ok, I am only a few days into using Linq and I am already getting annoyed.  I have a need to create an abstract base class that will have a few common properties (columns) that will be used by all my inherited classes.  Here is what I WAS wanting to do.

public abstract class StagedUserInformation
{

	[Column( DbType = "Int" )]
	public virtual Int32 UserID { get; set; }

	[Column( DbType = "VarChar(10)" )]
	public virtual string Pin { get; set; }
}
[Table( Name = "AccessKeyUserStagingForMyPI" )]
public class StagedUserInformationForMyPI : StagedUserInformation
{
	// no logic yet
}

You will notice that my 2 columns in the base class have the column attribute, and the inherited class has my table attribute.  This is exactly the way I want this to work.  However, when you try to do this you get the following error:
System.InvalidOperationException: Data member 'Int32 UserID' of type 'AccessKey.Domain.Entities.StagedUserInformation' is not part of the mapping for type 'StagedUserInformationForMyPI'. Is the member above the root of an inheritance hierarchy?

In order to get around this, here is what I had to do.

public abstract class StagedUserInformation
{
	public virtual Int32 UserID { get; set; }
	public virtual string Pin { get; set; }
}
[Table( Name = "AccessKeyUserStagingForMyPI" )]
public class StagedUserInformationForMyPI : StagedUserInformation
{

	[Column(DbType = "Int")]
	public override Int32 UserID { get; set; }

	[Column(DbType = "VarChar(10)")]
	public override string Pin { get; set; }
}

Having done this, I realized that I do not need an abstract class, but could use an interface so I made the change.

[Table( Name = "AccessKeyUserStagingForMyPI" )]
public class StagedUserInformationForMyPI : IStagedUserInformation
{
	[Column(DbType = "Int")]
	public Int32 UserID { get; set; }
	
	[Column(DbType = "VarChar(10)")]
	public string Pin { get; set; }
}

I guess what bothers me most is this concept seems pretty simple, but it has not been implemented.  And based on what I read (here) this was NOT implemented on purpose.

Till next time,


Published Mar 27 2008, 07:36 AM by Derik Whittaker
Filed under: ,

Comments

Kevin Berridge said:

Thanks for posting about this, I've been keeping my eye on Linq, hoping I'd be able to start moving toward it.  

It's one of those things that looks so good when you read about it on ScottGu's blog, but you have to worry about the devil in the details.

# March 27, 2008 10:21 AM

Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew

# March 28, 2008 9:38 AM

Frank said:

Yes, demos look cool but when you run into this when only creating ur first POC it's kinda dissapointing.. Anyway, if you want to keep control of your fixed properties in your baseclass you can use the override as below (with base.property).

       [Column]

       public override long ID

       {

           get

           {

               return base.ID;

           }

           set

           {

               base.ID = value;

           }

       }

# April 19, 2008 4:36 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News