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

  • SEARCH
  • Home
  • 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 1033495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:14:26+00:00 2026-05-16T14:14:26+00:00

Sorry about the title. I do realize it’s not very descriptive. :| Here is

  • 0

Sorry about the title. I do realize it’s not very descriptive. 😐

Here is my problem. I have a Table class that defines various properties found for a database table. Within this object I also have a property called PrimaryKey. PrimaryKey can either be of type PrimaryKey (I know, confusing) or CompositeKey. Evidently, a single column primary key consists of one column while a composite key consists of two or more columns.

/// <summary>
/// Defines what primary keys are supported.
/// </summary>
public enum PrimaryKeyType
{
    /// <summary>
    /// Primary key consisting of one column.
    /// </summary>
    PrimaryKey,
    /// <summary>
    /// Primary key consisting of two or more columns.
    /// </summary>
    CompositeKey,
    /// <summary>
    /// Default primary key type.
    /// </summary>
    Default = PrimaryKey
}

/// <summary>
/// Defines a database table entity.
/// </summary>
public class Table
{
    public Table()
    {
        Columns = new List<Column>();
    }

    public string Name { get; set; }
    public string Owner { get; set; }
    public AbstractPrimaryKey (What must the type be) PrimaryKey { get; set; }
    public IList<Column> Columns { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

/// <summary>
/// Defines a database column entity;
/// </summary>
public class Column
{
    public string Name { get; set; }
    public bool IsPrimaryKey { get; set; }
    public string DataType { get; set; }
    public bool IsNullable { get; set; }
}

public interface IPrimaryKey
{
    PrimaryKeyType KeyType { get; }
}

public interface IPk : IPrimaryKey
{
    Column KeyColumn { get; set; }
}

public interface ICompositeKey : IPrimaryKey
{
    IList<Column> KeyColumns { get; set; }
}

public abstract class AbstractPrimaryKey
{
    public abstract PrimaryKeyType KeyType { get; }
}

/// <summary>
/// Defines a primary key entity.
/// </summary>
public class PrimaryKey : AbstractPrimaryKey, IPk
{
    public override PrimaryKeyType KeyType
    {
        get { return PrimaryKeyType.PrimaryKey; }
    }

    public Column KeyColumn { get; set; }
}

/// <summary>
/// Defines a composite key entity.
/// </summary>
public class CompositeKey : AbstractPrimaryKey, ICompositeKey
{
    public CompositeKey()
    {
        KeyColumns = new List<Column>();
    }

    public override PrimaryKeyType KeyType
    {
        get { return PrimaryKeyType.CompositeKey; }
    }

    public IList<Column> KeyColumns { get; set; }
}

I am trying to make it that regardless of PrimaryKeyType, that accessing a specific Table object will give me access to property Columns from class CompositeKey.

How can I achieve this? If this isn’t possible, what alternatives do I have? I do understand that I could simply add IList<Column> Columns to IPrimaryKey. That however doesn’t seem very correct if I have a single column primary (which I know for a fact will always be one) inside a list. This is my first go at it so I’m sure there is room for improvement with this design.

  • 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-16T14:14:27+00:00Added an answer on May 16, 2026 at 2:14 pm

    It’s fine to add a KeyColumns property to AbstractPrimaryKey even though PrimaryKey will always have one value—at least the interface will be uniform. In ADO.NET, DataTable.PrimaryKey is an array of DataColumns (although modern guidlines dictate that it should be an IList or a collection class).

    The alternative would be to leave it as is and then check the PrimaryKeyType and cast to CompositeKey as needed. This would only make sense if you only need make the distinction in one place.

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

Sidebar

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.