EF 5 代码优先映射问题

EF 5 Code First Mapping Issue

有谁知道获取 EF、代码优先、映射两个具有相同名称但不同架构的 table 是否有诀窍?

这是我遇到的错误:

The type 'DAL.dbo.Employer' was not mapped. Check that the type has not been
explicitly excluded by using the Ignore method or NotMappedAttribute data
annotation. Verify that the type was defined as a class, is not primitive,
nested or generic, and does not inherit from EntityObject.

我正在努力,在停机期间,使用 EF 为我们现有的数据库整合一个新的 POC DAL,而不是我们现在拥有的遗留废话。一切都很顺利,直到我添加了一个与另一个 table 同名但架构不同的 table。

这里是原文table,参考其他table:

namespace DAL.dbo
{
   public partial class Employer
   {
     public Employer()
     {
       Employers = new List<Brokers.Employer>();
     }
     //table definition
     public virtual ICollection<Brokers.Employer> Employers { get; set;}
   }
}

这是新的 table,它与 dbo table 有一个 FK。:

namespace DAL.Brokers
{
  public partial class Employer
  {
    public Guid EmployerID { get; set;}
    //table definition
    public virtual dbo.Employer dboEmployer{ get; set;}
  }
}

对于那些可能需要它的人,这里是我的 OnModelCreating 方法的相关部分:

modelBuilder.Entity<Brokers.Employer>().HasKey(k => k.ID);
modelBuilder.Entity<Brokers.Employer>().ToTable("Employers", "Brokers");

modelBuilder.Entity<dbo.Employer>().HasKey(k => k.ID);
modelBuilder.Entity<dbo.Employer>().ToTable("Employers", "dbo");
modelBuilder.Entity<dbo.Employer>().HasMany(m => m.Employers).WithRequired(r => r.dboEmployer).HasForeignKey(f => f.EmployerID).WillCascadeOnDelete(false);

我将跳过所有细节,下面是 DbContext 的相关部分 class:

public class DALContext: DbContext
{
  public DALContext(string connection) : base(connection) {}

  public DbSet<Brokers.Employer> brokerEmployers { get; set;}

  public DbSet<dbo.Employer> dboEmployers { get; set;}

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    //see excerpt from above
  }
}

您需要使用唯一的 class 名称,即使模型 class 位于不同的命名空间中也是如此。这里有个解释:

EF Code first, how to register same table name with different schema?