2012-10-16 141 views
1

使用EF5代碼首先流利的API我在加載對象時收到此錯誤:「無效的列名Doctor_ID」。實體框架5無效的列名

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); 

    } 
}  

代碼成功執行下列:

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(); 

在此之後的代碼執行,其導致「無效的列名Doctor_ID」異常下列:

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

的代碼的第一行僅從用戶表中獲取數據,這就是爲什麼它成功執行,但爲什麼第二個EF5訪問它時未能將ID用作Doctor表的鍵?

注:我正在使用每種類型的TPT類型。

+0

了'Doctor'類是否有一個ID?我沒有做過太多的代碼第一件事,但它似乎是需要的映射工作。 – Gromer

回答

2

我不認爲問題出在上面的代碼上。我無法複製您的錯誤,並且CodeFirst從未在任何表上生成Doctor_ID列。還有沒有其他的關係,你沒有在這裏顯示被映射?

這裏就是整個控制檯應用程序:

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(); 
    } 

} 

其中創建該數據庫:

enter image description here

+0

謝謝,我會嘗試刪除關係並逐個添加它們,以查看其中一個是否會導致此問題。 – MWKZ

+1

刪除obj和bin文件夾解決了這個問題! – MWKZ