0
votes

I've used this code several times before and it always worked before, but in this particular project, I can't get Entity Framework to build my database.

When I checked Autos in my DbContext class, I found this:

Context can't be used while the model is being created

The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently.

Here's my 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());            
    }

And here's an example of one of those Configurations:

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

Can anyone help me to understand what's going on here? As I said, it's worked in every other project I've done this in, so why doesn't this work here?

Thanks in advance!

1

1 Answers

0
votes

What does the ToTable("Companies"); do?

I'm pretty sure that's where the problem is - you should not perform extra work besides creating the data context, especially when you call the context in that method.