Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 03-27-2008 7:36 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Kevin Berridge wrote re: Linq limitation with abstract base classes
on 03-27-2008 10:21 AM

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.

Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew
on 03-28-2008 9:38 AM

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

Frank wrote re: Linq2Sql limitation with abstract base classes
on 04-19-2008 4:36 PM

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;

           }

       }

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)