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

The Archive Base Latest Questions

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

I’ve got the following model and I want ShiftRequest and MissionRequest to have a

  • 0

I’ve got the following model and I want ShiftRequest and MissionRequest to have a single table in the DB.

    public class RequestBase
    {
        public int Id { get; set; }
        public DateTime? RequestDate { get; set; }
        public int UserId { get; set; }

        public virtual ICollection<Notification> Notifications { get; set; }

    }

    public class ShiftRequest : RequestBase
    {
        public virtual Column Column { get; set; }

    }

    public class MissionRequest : RequestBase
    {
        public virtual Mission Mission { get; set; }
    }

I’ve tried to do it in the override void OnModelCreating(ModelBuilder modelBuilder) method but only one RequestBases table is created:

modelBuilder.Entity<ShiftRequest>().MapSingleType().ToTable("dbo.ShiftRequests");
modelBuilder.Entity<MissionRequest>().MapSingleType().ToTable("dbo.MissionRequest");

What am I doing wrong?

EDIT

Column and Mission are also entities in my model, is that acceptable?

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

    Check the section about TPH in this article. If Mission and Column are complex types you will also find there how to map them. Generally you have to use MapHiearchy and Case methods instead of MapSingleType.

    Edit:

    Here is the example:

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration;
    
    
    namespace EFTest
    {
        public class RequestBase
        {
            public int Id { get; set; }
            public DateTime? RequestedDate { get; set; }
            public int UserId { get; set; }
        }
    
        public class Mission
        {
            public int Id { get; set; }
            public string Name { get; set; }
    
            public virtual ICollection<MissionRequest> MissionRequests { get; set; }
        }
    
        public class Column
        {
            public string Name { get; set; }
        }
    
        public class MissionRequest : RequestBase
        {
            public virtual Mission Mission { get; set; }
        }
    
        public class ShiftRequest : RequestBase
        {
            public Column Column { get; set; }
        }
    
        public class TestContext : DbContext
        {
            public DbSet<RequestBase> Requests { get; set; }
            public DbSet<Mission> Missions { get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.ContainerName = "EFTest";
                modelBuilder.IncludeMetadataInDatabase = false;
    
                // Example of complex type mapping. First you have to define 
                // complex type. Than you can access type properties in  
                // MapHiearchy.
                var columnType = modelBuilder.ComplexType<Column>();
                columnType.Property(c => c.Name).HasMaxLength(50);
    
                modelBuilder.Entity<Mission>()
                    .Property(m => m.Id)
                    .IsIdentity();
    
                modelBuilder.Entity<Mission>()
                    .HasKey(m => m.Id)
                    .MapSingleType(m => new { m.Id, m.Name })
                    .ToTable("dbo.Missions");
    
                modelBuilder.Entity<RequestBase>()
                    .Property(r => r.Id)
                    .IsIdentity();
    
                // You map multiple entities to single table. You have to  
                // add some discriminator to differ entity type in the table. 
                modelBuilder.Entity<RequestBase>()
                    .HasKey(r => r.Id)
                    .MapHierarchy()
                    .Case<RequestBase>(r => new { r.Id, r.RequestedDate, r.UserId, Discriminator = 0 })
                    .Case<MissionRequest>(m => new { MissionId = m.Mission.Id, Discriminator = 1 })
                    .Case<ShiftRequest>(s => new { ColumnName = s.Column.Name, Discriminator = 2 })
                    .ToTable("dbo.Requests");
            }
        }
    }
    

    Edit 2:

    I updated example. Now Mission is entity instead of complex type.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.