0

Given the following code example, how do you implement this in entity framework core where you do not want a table created for the base class but you do for the derived class and the primary key is defined in the base class?

public class JobBase
{
    public JobBase() { }

    public Guid JobId { get; set; } = Guid.NewGuid();

    public string Title { get; set; }
}

public class Job : JobBase
{
    public Job() { }

    public String AdditionalInformation { get; set; }
}

And here is what I have in my DBContext class:

public DbSet<Job> Jobs { get; set; }

var job = mb.Entity<Job>();
        job.HasKey(aa => aa.JobId);
        job.Property(aa => aa.JobId).HasColumnName("JobId");

I currently get the following error when trying to add-migration:

A key cannot be configured on 'Job' because it is a derived type. The key must be configured on the root type 'JobBase'. If you did not intend for 'JobBase' to be included in the model, ensure that it is not referenced by a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation on a type that is included in the model.

The only part of the error message I am doing is JobBase is referenced from a navigation on a type that is included in the model but doing that is the whole reason i implemented this relationship in the first place so i can't just remove that navigation.

scooter
  • 353
  • 4
  • 12
  • Have you tried `mb.Entity().HasKey(aa => aa.JobId);` instead of `mb.Entity().HasKey(aa => aa.JobId);` ? – 0xced Dec 18 '20 at 10:37
  • That does not work either. This causes EF to create a JobBase table which i do not want. – scooter Dec 18 '20 at 22:04
  • change the JobBase.JobId to JobBaseId or Id or add [Key] attribute on JobId. the default primary key rule is: the class should have "CLASSId" or "Id" property used for primary key. in other property naming type, you should use attribute "[Key]". Changing the JobId to Id is better solution. – mRizvandi Dec 22 '20 at 14:23

1 Answers1

-1
modelBuilder.Entity<Job>().HasKey(x => x.JobId).ToTable("Job");

EF Inheritance and Primary Keys

rashidali
  • 330
  • 1
  • 5
  • 16
  • The post you referenced is 7 years old. I'm not seeing a .ToTable option in my intellisense. Am i missing something or is this not available in Entity Framework Core? – scooter Dec 18 '20 at 05:21
  • Install Microsoft.EntityFrameworkCore.Relational – rashidali Dec 18 '20 at 05:23
  • Done. I'm still not getting intellisense for "ToTable" and if i go ahead and type it out anyway i get the following compilation error. I'll google this in the meantime but just in case you know what the problem is...'KeyBuilder' does not contain a definition for 'ToTable' and the best extension method overload 'RelationalEntityTypeBuilderExtensions.ToTable(EntityTypeBuilder, string)' requires a receiver of type 'EntityTypeBuilder' – scooter Dec 18 '20 at 05:43
  • Try removing it if its not required – rashidali Dec 18 '20 at 05:45
  • Try removing the Nuget package? I don't know if its required or not. The solution you provided looked promising but i do not currently know what to do to be able to call .ToTable() if that is indeed the solution. – scooter Dec 18 '20 at 05:52
  • Remove .ToTable(). modelBuilder.Entity().HasKey(x => x.JobId); – rashidali Dec 18 '20 at 05:53
  • That is what I originally had coded when i posted this post. So everything in my original post was with modelBuilder.Entity().HasKey(x => x.JobId); I thought that maybe adding the .ToTable("Job") might be the part that would fix my issue. – scooter Dec 18 '20 at 05:58