使用EF6 SQL Server CE代码创建数据库首先崩溃

时间:2014-05-21 23:12:57

标签: entity-framework sql-server-ce code-first entity-framework-6 dbcontext

我在运行应用程序时看到此错误

[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

代码:

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

参考文献:

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

上下文:

[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>

无法找到有关此错误的更多信息。似乎某些对象或某些配置中缺少连接属性,或者我正在使用的某个组件是不正确的版本。

更新

这非常疯狂..我已经在另一台机器上重新创建了解决方案(带有VS 2013的Windows 7 sp1)并从头开始获得了所有的nuget项目。

我通过手动创建数据库来绕过数据库创建问题,但现在开始看到此错误:

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

尝试启用迁移:

Enable-Migrations -ContextTypeName MvcTest.Data.AuditDb

投掷:

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

这是一连串的错误,没有尽头......

有没有人能够使用SQL CE Code First方法使EF 6工作?

1 个答案:

答案 0 :(得分:3)

我的解决方案中遇到了同样的问题。问题是我在解决方案的不同项目中有不同版本的EntityFramework。我的建议是从所有项目中删除EntityFramework dll,从packages.config中删除关于它的行,然后使用“Install-Package EntityFramework”nuget命令重新安装。祝你好运!