1

I'm new to EF Core and sorry if this is a stupid question. I'm getting the Invalid Column "Discriminator" when I try to query

Context.EmployeeAddressNavigation.where(x =>x.Name.Contains("name")).Select(x => new {x.Id,x.employAddress})

Employee

public class Employee
{
   public Guid Id {get;set;}
   public string Name {get;set;}
}

EmployeeAddress

public class EmployeeAddress
{
  public Guid EmployeeId {get;set;}
  public Guid AddressId {get;set;}
}

EmployeeAddressNavigation

public class EmployeeAddressNavigation : Employee
{
   public EmpoyeeAddress employAddress {get;set;}
}

I configured onModelCreate like this

override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Employee>().ToTable("employee");
    modelBuilder.Entity<EmployeeAddress>().ToTable("employeeaddress");
    modelBuilder.Entity<EmployeeAddressNavigation>()
            .HasMany(r => r.EmployeeAddress)
            .WithOne()
            .HasForeignKey(r => r.EmployeeId);
}

I saw similar question here EF Core “Invalid column name 'Discriminator'” error with inheritance but I did not understand how to fix my issue ? Can some one please also explain me how to use HasDiscriminator() and what is it's purpose?

kaleshanagineni
  • 325
  • 1
  • 4
  • 13
  • Is this all of your model? In your code `Select(x => new {x.Id,x.employAddress})` shows that the `x` type object has both an `Id` and `employAddress`, but none of your models have both of those properties. It seems like there is some information missing. – jwatts1980 Jan 18 '20 at 03:19
  • @jwatts1980 sorry, I've edited the question EmployeeAddressNavigation inherits the Employee. Thanks for your help. – kaleshanagineni Jan 18 '20 at 04:32
  • Here is another SO question that seems to be more related to yours. They added a `[NotMapped]` attribute to the derived class: https://stackoverflow.com/a/6586990/579148 – jwatts1980 Jan 18 '20 at 04:39
  • Infact I did tried that it did not work – kaleshanagineni Jan 18 '20 at 04:41
  • You added it here? `[NotMapped] public class EmployeeAddressNavigation : Employee { }` – jwatts1980 Jan 18 '20 at 04:42
  • Yes @jwatts1980 sorry for replying I was traveling – kaleshanagineni Jan 19 '20 at 03:11

2 Answers2

0

When there is inheritance hierarchy in your model you must specify discriminator.Like this

modelBuilder.Entity<Employee>()
            .HasDiscriminator<string>("EmployeeType");

To read more about discriminator,see The Fluent API HasDiscriminator Method

  • Thanks for your answer. Can you please explain What is significance of employeeType here it’s not in my DB table. – kaleshanagineni Jan 19 '20 at 03:12
  • EmployeeType is a column in your Employee table that ef core create to recognize which model inserted in your table.for example when ef core inserts Employee, value of employeeType is "Employee" and when inserts EmployeeAddressNavigation, value of employeeType is "EmployeeAddressNavigation". – Mehdi Javaheri Jan 19 '20 at 06:09
  • I do not have column in my sql table called EmployeeType so now I'm getting error invalid column employeetype – kaleshanagineni Jan 24 '20 at 00:07
  • EF added the discriminator implicitly as a shadow property.to read more about , see [Discriminator configuration](https://learn.microsoft.com/en-us/ef/core/modeling/inheritance#discriminator-configuration) – Mehdi Javaheri Jan 26 '20 at 10:37
0

I had the same issue and I thought it was a column named "discriminator" which I dont have. Using [NotMapped] on the child worked.

[NotMapped]
public class EmployeeAddressNavigation : Employee
{
   public EmpoyeeAddress employAddress {get;set;}
}
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113