1

使用 EF5 Code First fluent API 我收到此错误:加载对象时出现“无效的列名 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 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();

在此代码执行以下导致“Invalid column name Doctor_ID”异常之后:

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

第一行代码仅从 users 表中获取数据,这就是它成功执行的原因,但为什么在第二个 EF5 中,当它访问 Doctor 表时无法使用 ID 作为键?

注意:我使用每个类型 TPT 的类型。

4

1 回答 1

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

}

哪个创建了这个数据库:

在此处输入图像描述

于 2012-10-16T18:29:49.183 回答