4

I am trying to create a self-referencing org table in Entity Framework v6 code-first - and I can't seem to get it right... (so far I always worked with the EF 4.0 visual designer).

I have this class (that corresponds to a SQL Server table):

[Table("Organisation")]
public partial class Organisation
{
    public int OrganisationId { get; set; }
    public int? ParentOrgId { get; set; }

    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    [ForeignKey("ParentOrgId")]
    public virtual Organisation Parent { get; set; }
}

Pretty standard stuff - and this is my DbContext derived class which should set up this self-referencing hierarchy:

public partial class HierarchyCtx : DbContext
{
    public HierarchyCtx() : base("name=HierarchyCtx") 
    {
    }

    public virtual DbSet<Organisation> Organisation { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Organisation>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<Organisation>()
            .HasOptional(e => e.Parent)
            .WithMany()
            .HasForeignKey(m => m.ParentOrgId)
            .WillCascadeOnDelete(false); 
    }
}

Now I added a few rows to my SQL Server table, but when trying to load these entries, I get an error:

System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid column name 'ParentOrganisation_OrganisationId'.

Not entirely sure what's happening here.... what is Discriminator (a column I do not have) and why do I need it? How to add it? And why is EF trying to find a column ParentOrganisation_OrganisationId when I set the model up to use ParentOrgId as foreign key column??

This is a bit of a mystery to me..... anyone out there which has successfully done this in EF code-first before? What am I missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    `Discriminator` column is indication of object hierarchy. You have `partial` modifier on your entity class, are you sure there isn't additional code not shown here? Also the database is up to date reflecting the above entity setup? – Ivan Stoev May 27 '16 at 07:46
  • @IvanStoev: no, there's nothing else - no object hierarchy, no inheritance - this was generated by the EF wird "code-first from database" - so yes, I'm sure it's in sync with the database .... – marc_s May 27 '16 at 07:57
  • There must be something else. I copy/pasted the above in my EF test environment, run it once, it created the table with expected columns/keys, I've entered some data and then invoked `db.Organisation.ToList()` and it works w/o any problem. – Ivan Stoev May 27 '16 at 08:03
  • 1
    @IvanStoev: yeah - there is - stupid me ..... I wasn't descending `Organsation` from anything else - but I had a class that descended from `Organisation`, and that's what caused all of this..... thanks for the pointers! And additional brain and two more eyes most often help ! – marc_s May 27 '16 at 08:23
  • Hehe, no problem, it happens to all of us all the time :) – Ivan Stoev May 27 '16 at 08:24
  • @marc_s - Came across this in a cleanup. Looks like you found a solution (in the comments) and can self-answer + accept? – Alain Jan 04 '23 at 16:35

0 Answers0