MWKZ
MWKZ

Reputation: 124

Entity Framework 5 Invalid column name

Using EF5 Code First fluent API I receive this error: "Invalid column name Doctor_ID" when loading an object.

public class User
{
    public int ID { get; set; }
    ...
}

public class Doctor : User
{

    public int? TitleID { get; set; }
    public virtual Title Title { get; set; }
}


public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        this.ToTable("User", "Users");

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

        ...

    }
}

public class DoctorMap : EntityTypeConfiguration<Doctor>
{
    public DoctorMap()
    {
        // Table & Column Mappings            
        this.ToTable("Doctor", "Doctors");

        //this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.TitleID).HasColumnName("TitleID");            

        // Relationships
        this.HasOptional(t => t.Title)
            .WithMany(t => t.Doctors)
            .HasForeignKey(d => d.TitleID);

    }
}    

The code executes the followings successfully:

return (from item in this.DataProvider.Users
                    where
                        item.Username == username && item.Password == password
                    select new UserBasicInfo() { ID = item.ID, FirstName = item.FirstName, LastName = item.FirstName, Username = item.Username }).FirstOrDefault();

After this code executes the followings which cause the "Invalid column name Doctor_ID" exception:

User user = this.DataProvider.Users.SingleOrDefault(item => item.ID == id);

the first line of code gets data from the users table only and that is why it executes successfully but why in the second EF5 fails to use ID as key for Doctor table when it accesses it?

Note: I am using type per type TPT.

Upvotes: 1

Views: 3053

Answers (1)

Mark Oreta
Mark Oreta

Reputation: 10416

I don't think the problem is with the code you have above. I couldn't replicate your error, and CodeFirst never generated a Doctor_ID column on any table. Are there any other relationships that you haven't shown here being mapped?

Here's the entire console app:

public class User
{
    public int ID { get; set; }
}

public class Doctor : User
{
    public int? TitleID { get; set; }
    public virtual Title Title { get; set; }
}
public class Title
{
    public int TitleId { get; set; }
    public ICollection<Doctor> Doctors { get; set; }
}

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        this.ToTable("User", "Users");

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

public class DoctorMap : EntityTypeConfiguration<Doctor>
{
    public DoctorMap()
    {
        // Table & Column Mappings            
        this.ToTable("Doctor", "Doctors");

        //this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.TitleID).HasColumnName("TitleID");

        // Relationships
        this.HasOptional(t => t.Title)
            .WithMany(t => t.Doctors)
            .HasForeignKey(d => d.TitleID);

    }
}
public class Model : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Doctor> Doctors { get; set; }
    public DbSet<Title> Titles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new DoctorMap());
        modelBuilder.Configurations.Add(new UserMap());
    }
}

class Program
{
    public static void Main()
    {
        Model m = new Model();
        //var doctor = new Doctor();
        //m.Doctors.Add(doctor);
        //m.SaveChanges();
        var user = m.Users.SingleOrDefault(item => item.ID == 1);

        Console.WriteLine(user.ID);
        Console.ReadLine();
    }

}

Which created this database:

enter image description here

Upvotes: 2

Related Questions