Wcf EntityFramework.dll DbContext与Microsoft.data.Entity.CTP DbContext冲突

Wcf EntityFramework.dll DbContext与Microsoft.data.Entity.CTP DbContext冲突,wcf,c#-4.0,entity-framework-4,Wcf,C# 4.0,Entity Framework 4,从一个样本中抓取这个: protected override ObjectContext CreateDataSource() { NorthwindContext nw = new NorthwindContext(); // Configure DbContext before we provide it to the // data services runtime. nw.Configuration.Valida

从一个样本中抓取这个:

protected override ObjectContext CreateDataSource()
    {
        NorthwindContext nw = new NorthwindContext();

        // Configure DbContext before we provide it to the 
        // data services runtime.
        nw.Configuration.ValidateOnSaveEnabled = false;

        // Get the underlying ObjectContext for the DbContext.
        var context = ((IObjectContextAdapter)nw).ObjectContext;

        // Return the underlying context.
        return context;
    }       
修改它以使用我在项目中拥有的DbContext类

编辑:澄清我是从DbContext类强制转换的,正如示例所做的:

    public class NorthwindContext : DbContext
{
// Use the constructor to target a specific named connection string
public NorthwindContext()
    : base("name=NorthwindEntities")
{
    // Disable proxy creation as this messes up the data service.
    this.Configuration.ProxyCreationEnabled = false;

    // Create Northwind if it doesn't already exist.
    this.Database.CreateIfNotExists();
}
运行代码时,在转换DbContext的行中出现错误:

无法将“MyProject.MyDbContext”类型的对象强制转换为“System.Data.Entity.Infrastructure.IObjectContextAdapter”类型

尽管DbContext实现了IObjectContextAdapter:

public class DbContext : IDisposable, IObjectContextAdapter
我在SO和其他Google来源上发现了几个问题,但没有找到有效的解决方案

我正在使用EntityFramework4.2,试图升级到4.3beta版,我不确定这是否仍然存在

总体目标是将WCF中的数据作为数据服务提供

更新:深入挖掘,我发现EntityFramework.dll中的DbContext与Microsoft.data.Entity.CTP中的WCF项目中的类型之间存在歧义


不知道如何从这两个地方得到我想要的

提醒一下,这里的问题是EntityFramework.dll和Microsoft.Data.Entity.CTP之间的歧义导致我的DbContext的DataInitializer失去了功能

我通过在此处替换我的初始值设定项解决了此问题:

public class MyDataInitializer : RecreateDatabaseIfModelChanges<MyData>
{
    public void Seed(MyData context)
致:

我现在可以访问我的数据服务了


只有一个

能否检查您的NorthwindContext本身是否不是ObjectContext?是否可能对两个不同的EntityFramework.dll程序集有两个不同的引用?看起来您有两个版本的IObjectContextAdapter,其中一个无法转换为另一个。确保只有一个版本的EntityFramework.dll。我认为WCF数据服务是您正在尝试的10月份CTP吗?应使用最新版本的EntityFramework…@Pawel,这可能是可能的。我通过改变数据初始值设定项的继承方式解决了这个问题。我一直在等待,所以让我发布我的答案,你可以看看我是如何解决的。不过,在最初删除所有引用并重新添加它们之后,直到我进行了修复,才解决了这个问题。
    public class MyDataInitializer : IDatabaseInitializer<MyData>
{
    public void InitializeDatabase(MyData context)