Search code examples
c#postgresqllinqentity-framework-6npgsql

EntityFramework6 - Error 42703: column Extent1... not found


so I encountered this Error:

"ExceptionMessage": "42703: column Extent1.Employee_mavnr does not exist"

and after some Google Searches, I still can't figure the exact problem out. Most explanation I found, were about column names and i quite changed them a few times now, but it is still not working.

My Database looks like this

CREATE TABLE tbl_process
(
  id integer NOT NULL DEFAULT nextval('"Process_Id_seq"'::regclass),
  name text,
  description text,
  submitter text,
  created date,
  CONSTRAINT "PrimaryKey-ID" PRIMARY KEY (id)
)
CREATE TABLE v_employee
(
  mavnr text NOT NULL, -- Mitarbeiter Nummer
  vorname text,
  nachname text,
  abt text,
  email text,
  del boolean,
  CONSTRAINT employee_pkey PRIMARY KEY (mavnr)
)

And my current Model which generates this error looks like this:

 [Table("tbl_process", Schema = "public")]
    public class Process
    {
        [Key]
        public int id { get; set; }

        public string name { get; set; }

        public string description { get; set; }

        public DateTime created { get; set; }

        public string submitter { get; set; }

        public virtual Employee Employee { get; set; }
    }


    [Table("v_employee", Schema = "public")]
    public class Employee
    {
        [Key]
        public string mavnr { get; set; }

        public string vorname { get; set; }

        public string nachname { get; set; }

        public string abt { get; set; }

        public string email { get; set; }

        public bool del { get; set; }
    }

The Request for the Employees runs through without any Error, only the Process Table is making errors. I hope someone see what is wrong with it.

Thanks for your help, really appreciate it

Edit:

The submitter column in the process table should link to the mavnr from the employee table


Solution

  • As @DavidG said in the above comment, you have a navigation property but no foreign key property for that navigation.

    You need to add a FK property and mark the navigation property with [ForeignKey("keyname")] attribute:

    [Table("tbl_process", Schema = "public")]
    public class Process
    {
        [Key]
        public int id { get; set; }
    
        public string name { get; set; }
    
        public string description { get; set; }
    
        public DateTime created { get; set; }
    
        public string submitter { get; set; }
    
        public string mavnr { get; set; } // <-- add this foreign key
    
        [ForeignKey("mavnr")] // <-- decorate the navigation property like this (or is "submitter" your FK?)
        public virtual Employee Employee { get; set; }
    }
    
    
    [Table("v_employee", Schema = "public")]
    public class Employee
    {
        [Key]
        public string mavnr { get; set; }
    
        public string vorname { get; set; }
    
        public string nachname { get; set; }
    
        public string abt { get; set; }
    
        public string email { get; set; }
    
        public bool del { get; set; }
    }