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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:19:34+00:00 2026-06-01T18:19:34+00:00

I’m attempting a little EF with code first – and I can’t figure out

  • 0

I’m attempting a little EF with code first – and I can’t figure out where I have gone wrong with the is simply example of my own. I’m just out of ideas, and would like to nail down where I’m going wrong…

First the simply POCO class representing a location – a location can be a RadioStation, or a Merchant. I haven’t added additional fields (which will come later), so right now it’s just a TPH in as simple config as I can make it.

namespace EFDataClasses.Entities
{

  public class RadioStation : Location
  {
    public RadioStation()
    {
    }

  }

  public class Merchant : Location
  {
    public Merchant()
     {
     } 
   }


  public class Location
  {
   public Location()
   {

   }

public int Loc_ID { get; set; }
public string Loc_Code { get; set; }
public string Loc_Name { get; set; }
public string  Loc_Type {get;set;}

 }
}

then the config class:

namespace EFDataClasses.Mapping
{


  public class LocationMap : EntityTypeConfiguration<Location>
  {
    public LocationMap()
    {
      // Primary Key
      this.HasKey(t => t.Loc_ID);

      // Properties
      this.Property(t => t.Loc_ID)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

      // Properties
      this.Property(t => t.Loc_Code)
          .IsRequired()
          .HasMaxLength(50);

      this.Property(t => t.Loc_Name)
          .IsRequired()
          .HasMaxLength(50);


      this.Property(t => t.Loc_ID).HasColumnName("Loc_ID");
      this.Property(t => t.Loc_Code).HasColumnName("Loc_Code");
      this.Property(t => t.Loc_Name).HasColumnName("Loc_Name");

      //my discriminator property
      this.Property(t => t.Loc_Type).HasColumnName("Loc_Type").HasColumnType("varchar").HasMaxLength(50).IsRequired();

      // Table & Column Mappings
      this.Map(m =>
      {
        m.ToTable("Location");
        m.Requires("Loc_Type").HasValue("Location");
      }
        )
        .Map<RadioStation>(m =>
        {
          m.ToTable("Location");
          m.Requires("Loc_Type").HasValue("RadioStation");
        }
        )
        .Map<Merchant>(m =>
        {
          m.ToTable("Location");
          m.Requires("Loc_Type").HasValue("Merchant");
        }
        )
        ;



    }
  }
}

here is the context:

namespace EFDataClasses
{
  public class MyContext : DbContext
  {
    static MyContext()
    {
      Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());
    }

    public DbSet<EFDataClasses.Entities.Location> Locations {get; set;}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Configurations.Add(new LocationMap());
    }
  }
}

and finally the program class which attempts to add radio station..

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

namespace EFConsole
{
  class Program
  {
    static void Main(string[] args)
    {

      var db = new EFDataClasses.MyContext();
      db.Locations.Add(new EFDataClasses.Entities.RadioStation() { Loc_Name = "Radio Station Name 1",  Loc_Code = "RD1" });
      int chngs = db.SaveChanges();
      System.Diagnostics.Debugger.Break();

    }
  }
}

The error that I’m getting is a validation error on Loc_Type saying that it’s a required field. My impression here is that EF would fill that in when I select the appropriate type – and all my reading supports that.

If I do add the appropriate location type – EF give me another error….

arggghhh!

In the end I would like to make Location abstract but does that mean I can drop the hasvalue(“Location”)?

I would like to move on here, but I’m curious where I have done wrong. thanks!

  • 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-01T18:19:36+00:00Added an answer on June 1, 2026 at 6:19 pm

    The problem is that when you use a column as a discriminator for TPH mapping you cannot also map that column to a property in your class. This is because EF is now controlling the value of that column based on the type of .NET class. So you should remove this line that does the mapping of the Loc_Type property to the Location column:

    // Remove this line:
    this.Property(t => t.Loc_Type).HasColumnName("Loc_Type").HasColumnType("varchar").HasMaxLength(50).IsRequired();
    

    If you need (or want) to specify an explicit column type, size, etc for the discriminator column then you can do that in the Map call. For example:

    Map(m =>
    {
        m.ToTable("Location");
        m.Requires("Loc_Type")
            .HasValue("Location")
            .HasColumnType("varchar")
            .HasMaxLength(50)
            .IsRequired();
    }
    

    You don’t need to have any property in the class representing the location type. If you do want to get a string value equivalent of Loc_Type then you can use something like this in Location:

    public virtual string Loc_Type
    {
        get { return "Location"; }
    }
    

    Then override it in the other classes. For example:

    public override string Loc_Type
    {
        get { return "RadioStation"; }
    }
    

    The rest of the code looks fine.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.