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 9305827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:01:13+00:00 2026-06-19T00:01:13+00:00

I have a shared type that I’d like to represent in a database. I

  • 0

I have a shared type that I’d like to represent in a database. I need to know either if it’s possible with code first or if it’s bad design. (Please point to sources of your reasoning).

Here’s the layout:

public class A
{
    public Guid Id;
    public Guid ParentId; // Points to either B or C
    public string Foo;
}

public class B
{
    public Guid Id;
    public virtual ICollection<A> ManyA { get; set; }
    // Other fields
}

public class C
{
    public Guid Id;
    public virtual ICollection<A> ManyA { get; set; }
    // Other fields
}

I’ve managed GUID PKs by using the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation, which I believe would make this possible.

The goal is to keep the A-table simple; I’d like it not to have B_Id and C_Id columns, as I think they’re unnecessary, and there may be a D type in the future that has many A just like B and C. Also, this scenario is used for a few other relationships.

I think the spirit of the problem is apparent, however the EF-specific class members are just there for illustration. If they need changing, so be it.

(I’m sorry for the contrived example, however exposing the true nature of this, IMO, doesn’t clarify the problem).

Also, I tried searching for hours! I don’t know the proper nomenclature to find a suitable answer. I’m pretty new to databases and EF in general.

  • 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-06-19T00:01:15+00:00Added an answer on June 19, 2026 at 12:01 am

    I’ve found that I can make a common base type for both B and C types. For example:

    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new Ctx())
            {
                var b = new B();
                var c = new C();
                b.ManyA.Add(new A());
                c.ManyA.Add(new A());
    
                context.Cs.Add(c);
                context.Bs.Add(b);
    
                context.SaveChanges();
            }
        }
    }
    
    public class Ctx : DbContext
    {
        public DbSet<A> As { get; set; }
        public DbSet<B> Bs { get; set; }
        public DbSet<C> Cs { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BaseClass>()
                .HasMany<A>(b => b.ManyA)
                .WithRequired(a => a.BaseObject)
                .WillCascadeOnDelete(true);
        }
    }
    
    public class BaseClass
    {
        public BaseClass() { ManyA = new HashSet<A>(); }
    
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public virtual ICollection<A> ManyA { get; set; }
    }
    
    public class A
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public BaseClass BaseObject { get; set; }
        public string Foo { get; set; }
    }
    
    public class B : BaseClass
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public string SomeProperty { get; set; }
    }
    
    public class C : BaseClass
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public int SomeOtherProperty { get; set; }
    }
    

    And, based on this Msdn article, would result in Table Per Hierarchy database layout. TPH performs better versus TPT, which I was thinking could be an answer.

    I was really hoping for something simpler, but performance is the bottom line, afterall

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

Sidebar

Related Questions

So when I have code like: shared_ptr<Foo> bar (my_normal_operator<Foo>(mumble)); Even though the type Foo
If I have classes that need to be shared between my webapp and Tomcat
I have code that gives a list of all possible values for any given
I have shared hosting on CentOS with local MySQL database. I'm able to run
I have a shared hosting where I have my website and MySQL database. I've
I have some shared code I want to reuse in my Xcode projects. The
I have a database that is holding real estate MLS (Multiple Listing Service) data.
It is possible to have WCF Client that connects persistantly using duplex over net.tcp
Java: I have shared variable(of type Object) whose value will be changed very frequently
My question is about the Type of db that is on a shared hosting

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.