Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

SQLException: Invalid column name

I have recently converted my existing database to code using EF Reverse Engineer Code First. After polishing most of the results, I've started writing tests. The error I'm currently having is trying to access a column name called 'Element_ElementCode', however no such column exists.
To be sure of this, I've performed a search on my entire project to avoid the possibility of accidentally declaring it as such.

Find all "Element_ElementCode", Subfolders, Find Results 1, Entire Solution, ""
Matching lines: 0    Matching files: 0    Total files searched: 120

The exact error is as follows:

System.Data.SqlClient.SqlException: Invalid column name 'Element_ElementCode'.

Invalid column name 'Element_ElementCode' is repeated 10-15 times and the stacktrace doesn't provide any clue.

This exception occurs when executing a test which contains an expression to retrieve data and performs some asserts onto it.

var doos = (dbContext.Elementen.Where(d => d.ElementCode == "DOOS9001")).FirstOrDefault();

This is the result from a query in SQL Server itself: enter image description here

Element (inside Element.cs) has the following fields:

ElementCode
Doelgroep
Type
Omschrijving
Titel

Elements are mapped like this:

public class ElementenMap : EntityTypeConfiguration<Element> {
        public ElementenMap() {

            // Primary Key
            this.HasKey(t => t.ElementCode);

            // Properties
            this.Property(t => t.ElementCode)
                .IsRequired()
                .HasMaxLength(255);

            this.Property(t => t.Type)
                .HasMaxLength(31);

            this.Property(t => t.Doelgroep)
                .HasMaxLength(255);

            this.Property(t => t.Omschrijving)
                .HasMaxLength(255);

            this.Property(t => t.Titel)
                .HasMaxLength(255);

            // Table & Column Mappings
            this.ToTable("Elementen");
            this.Property(t => t.ElementCode).HasColumnName("ElementCode");
            this.Property(t => t.Type).HasColumnName("Type");
            this.Property(t => t.Doelgroep).HasColumnName("Doelgroep");
            this.Property(t => t.Omschrijving).HasColumnName("Omschrijving");
            this.Property(t => t.Titel).HasColumnName("Titel");

            // Relationships
            this.HasMany(t => t.Kernwoorden)
                .WithRequired(t => t.Element)
                .Map(m => m.ToTable("Kernwoorden"));
        }
    }

Furthermore: I am certain the database is accessed because other tests on different tables inside the same database succeed.

I've tried to provide as much relevant info as possible, let me know if I've forgotten a source. Why is it trying to access a column called Element_ElementCode? Is it perhaps a convention I've forgotten to turn off (I've already had to turn off the PluralizingTableNameConvention)?

What direction should I be looking for this?

Edit:

Kernwoord.cs

public class Kernwoord {
        public string ElementCode { get; set; }

        public string KernwoordString { get; set; }

        public virtual Element Element { get; set; }
    }

Element.cs

public partial class Element {
        public Element() {
            this.LeertrajectElementen = new List<LeertrajectElement>();
            this.Kernwoorden = new List<Kernwoord>();
        }

        public string ElementCode { get; set; }

        public string Type { get; set; }

        public string Doelgroep { get; set; }

        public string Omschrijving { get; set; }

        public string Titel { get; set; }

        public virtual Casus Casus { get; set; }

        public virtual Document Document { get; set; }

        public virtual Doos Doos { get; set; }

        public virtual ICollection<LeertrajectElement> LeertrajectElementen { get; set; }

        public virtual StellingenSpel StellingenSpel { get; set; }

        public virtual ICollection<Kernwoord> Kernwoorden { get; set; }
    }

LeertrajectElementenMap.cs snippet:

 this.HasRequired(t => t.Element)
     .WithMany(t => t.LeertrajectElementen)
     .HasForeignKey(d => d.ElementCode);

EDIT:

The problem was solved by fixing the existing inheritance issues.

Upvotes: 3

Views: 17295

Answers (2)

Slauma
Slauma

Reputation: 177133

You don't specify the foreign key column name of your relationship. It should be:

this.HasMany(t => t.Kernwoorden)
    .WithRequired(t => t.Element)
    .Map(m => m.MapKey("ElementCode")); // FK column name in Kernwoorden table

Or, if you have a foreign key property ElementCode in Kernwoorden class:

this.HasMany(t => t.Kernwoorden)
    .WithRequired(t => t.Element)
    .HasForeignKey(t => t.ElementCode);

Upvotes: 2

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65361

There is a covention with respect to types.

For example if you have a customer and that customer has one and only one Adress, and the Adress has a Postcode.

Then it will generate a table Customer with a field Adress_Postcode.

In your case your Class name and Table name are different, so it is putting the type Element into table Elementen.

Upvotes: 0

Related Questions