从样本中获取:
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类。
EDIT:澄清我正在像示例一样从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.IOBbjectContextAdapter"类型。
尽管DbContext实现了IObjectContextAdapter:
public class DbContext : IDisposable, IObjectContextAdapter
我在SO和其他谷歌资源上发现了几个问题,但没有找到有效的解决方案。
我使用的是实体框架4.2,试图更新到4.3测试版,我不确定是否坚持了下来。
总体目标是将WCF中的数据作为DataService提供。
更新:深入挖掘,我发现我的DbContext是什么(来自EntityFramework.dll)和WCF项目中的类型(来自Microsoft.data.Entity.CTP)之间存在歧义问题
不知道如何从这里得到我想要的。。。。
提醒一下,这里的问题是EntityFramework.dll和Microsoft.Data.Entity.CTP之间的模糊性导致我为DbContext提供的DataInitializer失去功能。
我通过在这里替换Initializer解决了这个问题:
public class MyDataInitializer : RecreateDatabaseIfModelChanges<MyData>
{
public void Seed(MyData context)
收件人:
public class MyDataInitializer : IDatabaseInitializer<MyData>
{
public void InitializeDatabase(MyData context)
现在我可以访问我的DataService了。
只有一个