C# 首先在EF代码中构建数据库,在创建模型时不能使用上下文

C# 首先在EF代码中构建数据库,在创建模型时不能使用上下文,c#,entity-framework,C#,Entity Framework,我以前曾多次使用过这段代码,而且它以前一直有效,但在这个特定的项目中,我无法使用Entity Framework来构建我的数据库 当我在DbContext类中检查Autos时,我发现: 创建模型时无法使用上下文。如果上下文在OnModelCreating方法内使用,或者如果多个线程同时访问同一上下文实例,则可能引发此异常 以下是我的OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBu

我以前曾多次使用过这段代码,而且它以前一直有效,但在这个特定的项目中,我无法使用Entity Framework来构建我的数据库

当我在DbContext类中检查Autos时,我发现:

创建模型时无法使用上下文。如果上下文在OnModelCreating方法内使用,或者如果多个线程同时访问同一上下文实例,则可能引发此异常

以下是我的OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new CompanyDBConfiguration());
        modelBuilder.Configurations.Add(new ClientDBConfiguration());
        modelBuilder.Configurations.Add(new QuoteDBConfiguration());
        modelBuilder.Configurations.Add(new RoleDBConfiguration());
        modelBuilder.Configurations.Add(new UserDBConfiguration());
        modelBuilder.Configurations.Add(new InvoiceDBConfiguration());
        modelBuilder.Configurations.Add(new JobDBConfiguration());
        modelBuilder.Configurations.Add(new JobStatusDBConfiguration());            
    }
下面是其中一种配置的示例:

public class CompanyDBConfiguration : DBBaseObject<Company>
{

    public CompanyDBConfiguration()
        : base()
    {
        Property(p => p.Companame)
            .HasColumnName("sCompanyname")
            .IsRequired();

        Property(p => p.ContactPerson)
            .HasColumnName("sContactPerson")
            .IsRequired();

        Property(p => p.ContactNumber)
            .HasColumnName("sContactNumber")
            .IsRequired();

        Property(p => p.EmailAddress)
            .HasColumnName("sEmailAddress")
            .IsRequired();

        Property(p => p.PhysicalAddress)
            .HasColumnName("sPhysicalAddres")
            .IsRequired();

        Property(p => p.LicenseKey)
            .HasColumnName("sLicenseKey")
            .IsRequired();

        Property(p => p.LicenseDate)
            .HasColumnName("dtLicenseDate")
            .HasColumnType("datetime")
            .IsRequired();

        ToTable("Companies");
    }
}
有人能帮我了解这里发生了什么吗?正如我所说的,它在我做过的每一个项目中都起作用,那么为什么在这里不起作用呢


提前谢谢

什么是Totable公司;是吗


我很确定这就是问题所在-除了创建数据上下文之外,不应该执行额外的工作,尤其是在该方法中调用上下文时。

作为最后的努力,我完全注释掉了OnModelCreating方法,它成功了。例如,我还有一些其他问题需要整理空datetime值,这些值会给我关于关系的错误,但现在一切正常,这不是一个很好的主意。您报告的问题很可能是此操作的结果。你能告诉我这个方法是怎么回事吗?