Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:44:18+00:00 2026-05-17T06:44:18+00:00

This is what I have so far using System; using System.Collections.Generic; using System.Data.Linq; using

  • 0

This is what I have so far

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

namespace Firelight.Business
{
    public interface IBaseEntity<K>
    {
        K Id { get; }
    }

    /// <summary>
    /// Base business database connection object, primary key is int
    /// </summary>
    /// <typeparam name="T">Table name</typeparam>
    public abstract class BaseEntity<T> : BaseEntity<T, Guid> where T : class, IBaseEntity<Guid>
    {
    }

    /// <summary>
    /// Base business database connection object
    /// </summary>
    /// <typeparam name="T">Table name</typeparam>
    /// <typeparam name="K">Primary key type</typeparam>
    public abstract class BaseEntity<T,K> : IBaseEntity<K> where T : class, IBaseEntity<K>
    {
        // Avoids having to declare IBaseConnection at partial class level
        [Column(Name = "Id", CanBeNull = false, IsPrimaryKey = true, IsDbGenerated = true)]
        public K Id { get; set; } // { return default(K); }

        public static Table<T> Table 
        {
            get { return LinqUtil.Context.GetTable<T>(); } 
        }

        public static T SearchById(K id)
        {
            return Table.Single<T>(t => t.Id.Equals(id));
        }

        public static void DeleteById(K id)
        {
            Table.DeleteOnSubmit(SearchById(id));
            LinqUtil.Context.SubmitChanges();
        }
    }
}

My problem is that mapping doesn’t work:

Data member ‘System.Guid [or System.Int32] Id’ of type
‘X’
is not part of the mapping for type
‘X’. Is the member above
the root of an inheritance hierarchy?

Before trying to map the attributes, I got this instead:

Could not find key member ‘Id’ of key
‘Id’ on type ‘X’. The key may be wrong
or the field or property on ‘X’ has
changed names.

I tried changing K to Guid and it works, but why? I don’t see how generic-typing is an issue here

I’m not entirely sure that I actually needed the Interface either, I don’t really remember why I added it.

So, the question would be: How can I make a class like this work? I want it so I can access a commonly named PK (Id), which always has the type K [which is Guid or Int32], and refactor basic functions like select and delete by Id

Thanks!

EDIT:

this works

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;

namespace Firelight.Business
{
    public interface IBaseEntity<K>
    {
        K Id { get; set; }
    }

    /// <summary>
    /// Base business database connection object
    /// </summary>
    /// <typeparam name="T">Table name</typeparam>
    public abstract class BaseEntity<T> : IBaseEntity<Guid> where T : class, IBaseEntity<Guid>
    {
        // Avoids having to declare IBaseConnection at partial class level
        public Guid Id { get; set; }

        public static Table<T> Table 
        {
            get { return LinqUtil.Context.GetTable<T>(); } 
        }

        public static T SearchById(Guid id)
        {
            return Table.Single<T>(t => t.Id.Equals(id));
        }

        public static void DeleteById(Guid id)
        {
            Table.DeleteOnSubmit(SearchById(id));
            LinqUtil.Context.SubmitChanges();
        }
    }
}

What I want would be basically the same, replacing Guid with K and making the class BaseEntity (so I can use the same class for Int32 and Guid PKs

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:44:19+00:00Added an answer on May 17, 2026 at 6:44 am

    What you are seeking to do will not work with LINQ to SQL. To use inheritance with LINQ to SQL, you have to make use of the [InheritanceMapping] attribute on your base class. Let’s say you have a base class called Vehicle and a sub-class called Motorcycle:

    [InheritanceMapping(Type = typeof(Motorcycle), IsDefault = true, Code = 1)]
    [Table]
    public class Vehicle
    {
        [Column]
        public string Make { get; set; }
    
        [Column]
        public string Model { get; set; }
    
        [Column(IsDiscriminator = true, Name="VehicleTypeId")]
        public VehicleType VehicleType { get; set; }
    }
    
    public class Motorcycle : Vehicle
    {
        // implementation here
    }
    

    In order to make this inheritance work in LINQ to SQL, you have to apply the [InheritanceMapping] to the base class and you also have to have a discriminator column (e.g., VehicleType in the above example). Notice the Code in the InheritanceMapping is “1” – that means if the VehicleType from the database is “1” then it’ll create the Motorcycle sub-class. You apply one [InheritanceMapping] attribute on the base class for each sub-class you’re supporting.

    From a purist standpoint, this is a violation of OO because the base class knows about it’s sub-classes. That bit of weirdness typically puts people off a little about how LINQ to SQL implements inheritance. But there you have it.

    Update
    This works:

    public abstract class BaseEntity<T, K> : IBaseEntity<K> where T : class, IBaseEntity<K>
    {
        public abstract K Id { get; set; }
    
        public static Table<T> Table
        {
            get { return context.GetTable<T>(); }
        }
    
        public static T SearchById(K id)
        {
            return Table.Single<T>(t => t.Id.Equals(id));
        }
    
        public static void DeleteById(K id)
        {
            Table.DeleteOnSubmit(SearchById(id));
            context.SubmitChanges();
        }
    }
    

    Notice the difference is that I don’t have any [Column] attribute on the Id property. Also notice I made it abstract. The implementing class looks like this:

    [Table(Name = "dbo.Contacts")]
    public class Contact : BaseEntity<Contact, int>
    {
        [Column]
        public override int Id { get; set; }
        [Column]
        public string FirstName { get; set; }
        [Column]
        public string LastName { get; set; }
    }
    

    Notice the Id property in this class does have a [Column] attribute and I’m overriding the abstract propety. So I verified that works.

    Having said that, there are a couple of reasons why I question your current design. First you’ve got your data access methods as part of your entity and many people (including myself) consider this a violation of Separation of Concerns. You could introduce the Repository pattern here and have a repository for each entity – make that repository generic based on the type and key. The other thing that is weird about the above approach is that the BaseEntity has an Id property and the sub-class (in my example the Contact class) also has a property of Id (to make LINQ To SQL happy). Had to make the Id property in the base class abstract and then override it in the implementor. This violates DRY because I’ll have to do this for each entity. But this is another thing that is the result of hoops you have to jump through to make LINQ to SQL happy. But it will work! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I were to have something like: namespace SomeNameSpace { #region Usings using System.Collections.Generic;
I have been using this so far system 'strings binary-file.dmp | grep search_string' Is
I'm using the javascript module pattern, and have this so far: var APP; if(APP
This is what I have so far - my dropbox public URL creation script
I have this so far but the Column 'name' is ambiguous, so I want
I have this so far but I don't know how to write over the
I have this so far: http://jsfiddle.net/u5vhS/54/ . What I want to be able to
So far i have this: mov ah,02h mov cl,11001100001111011101000b ;6,692,584 in dec mov dl,0
Thanks to everyone out there helping newbies like me. So far I have this:
This is what I have so far: [my1@graf home]$ curl -# -o f1.flv 'http://osr.com/f1.flv'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.