4
votes

I have 2 solutions, using EF 6.0 and both use the exact same default configuration. Still, they connect to 2 different data sources!? (localdb)\mssqllocaldb and .\SQLEXPRESS.

My configuration:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

The DbContext is also similar for both:

public class PlusUltraContext : DbContext
{
    public PlusUltraContext() : base("PlusUltra")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Models.Article> Articles { get; set; }
    public DbSet<Models.ArticleComment> Comments { get; set; }
}

vs

public class InvoicingContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<Company> Companies { get; set; }
    public DbSet<Country> Countries { get; set; }
    public DbSet<Currency> Currencies { get; set; }
    public DbSet<Invoice> Invoices { get; set; }
    public DbSet<InvoiceLine> InvoiceLines { get; set; }
    public DbSet<Person> Persons { get; set; }
    public DbSet<Unit> Units { get; set; }

    public InvoicingContext() : base("Invoicing")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }
}

However, when I run the Update-Database command, the first project connects to .\SQLEXPRESS, while the other connects to (localdb)\mssqllocaldb

Target database is: 'PlusUltra' (DataSource: (localdb)\mssqllocaldb, Provider: System.Data.SqlClient, Origin: Convention).

vs

Target database is: 'Invoicing' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention).

My Question

What are some things I can check to see why they behave differently?

3

3 Answers

6
votes

I found the answer, thanks to this post:

EF6 can't find LocalDBConnectionFactory

In the solution where .\SQLEXPRESS was used, the startup project was another class library, which DIDN'T contain the EF configuration files. As soon as I set my startup project to the one containing the configuration files, everything worked as expected, and mssqllocaldb was used.

0
votes

Is hard to know why but here are some possible reasons

  1. Your software depends actually on two data sources (one has information related to certain clients/information and the other has other complementary information) Many systems or designs depend on more than 1 database or datasource

  2. The person who created that solution has a local testing environment and a production environment and might comment or switch between the two by other means

Things to check ?...well find if the two databases have the exact same TABLE NAMES, STORED PROCEDURES etc ...basically determine if the two datasources are exactly the same or not.

If they are not the same is because one is for invoicing and the other for something else.

0
votes

One more thing to check is that if you have 2 projects in your solution. Make sure that it's got the right one. If you type update-database -verbose it will say Using Startup project. If it's the wrong one go to the solution properties and change it.

Having the wrong one can select the wrong database and can do some pretty weird things. For example I had a few models - 2 were working ok with the right db but the 3rd didn't. Can't figure out why, but when I sorted the startup project all came back to normality.