使用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類型。
了'Doctor'類是否有一個ID?我沒有做過太多的代碼第一件事,但它似乎是需要的映射工作。 – Gromer