Sonic Soul
Sonic Soul

Reputation: 24979

Creating a database with EF6 SQL Server CE Code First crashing

I am seeing this error when I run my app

[MissingMethodException: Method not found: 'System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher System.Data.Entity.Infrastructure.Interception.DbDispatchers.get_Connection()'.]
   System.Data.Entity.SqlServerCompact.SqlCeProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 timeOut, StoreItemCollection storeItemCollection) +0
   System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +75
   System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +132

Code:

var db = new AuditDb();
db.Database.CreateIfNotExists(); // blows up! 
or
db.Set<CampaignAudit>().Find(0); // same error

References:

EntityFramework:                  6.0.0.0
EntityFramework.SqlServerCompact: 6.0.0.0
System.Data:                      4.0.0.0
System.Data.SqlServerCe:          4.0.0.0

Context:

[DbConfigurationType("MvcTest.Data.AuditDBConfiguration, MvcTest")]
public class AuditDb : DbContext
{
    // tried this approach as well:
    // static AuditDb() { Database.SetInitializer(new CreateDatabaseIfNotExists<AuditDb>()); }
    public AuditDb() : base ("Name=AuditDB"){}
    public DbSet<CampaignAudit> Audits { get; set; }

    protected override void OnModelCreating(DbModelBuilder builder)
    {

        Map<CampaignAudit>(builder);
        ...
}

DBConfiguration:

public class AuditDBConfiguration : DbConfiguration
{
    public AuditDBConfiguration()
    {
        SetProviderServices(
            SqlCeProviderServices.ProviderInvariantName,
            SqlCeProviderServices.Instance
        );
        SetDefaultConnectionFactory(
            new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName)
        );
    }
}

Web.Config:

<add name="AuditDB" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=C:\logs\GroundAuditDb.sdf" />

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
<DbProviderFactories>
  <remove invariant="System.Data.SqlServerCe.4.0" />
  <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>

Can't find much about this error. Seems that a connection property is missing on some object, or in some config, or some component I am using is incorrect version.

update

this is pretty crazy.. I've re-created the solution on another machine (windows 7 sp1 with VS 2013) and got all the nuget projects from scratch.

I bypassed database create problem by creating the database manually, but now started seeing this error:

Could not load type 'System.Data.Entity.Migrations.Model.AlterTableOperation' from assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

so tried enabling migrations:

Enable-Migrations -ContextTypeName MvcTest.Data.AuditDb

which is throwing:

Method not found: 'Void System.Data.Entity.ModelConfiguration.Configuration.EntityMappingConfiguration`1.MapInheritedProperties()

it has been a stream of errors with no end in sight..

has anyone been able to make EF 6 work with SQL CE Code First approach??

Upvotes: 2

Views: 2848

Answers (1)

Stefan Turcanu
Stefan Turcanu

Reputation: 924

I had the same problem in my Solution. The problem was that I had different versions of EntityFramework in different projects in the solution. My recommendation is that you remove the EntityFramework dll from all the projects, remove the line about it from the packages.config and then reinstall using the "Install-Package EntityFramework" nuget command. Good luck!

Upvotes: 3

Related Questions