WCF 数据服务和实体框架代码优先?
WCF 数据服务是否支持使用实体框架代码优先 (4.1)?似乎很难理解如何使用 DbSet
或 DbContext
。我想这应该不会太令人惊讶,因为 EFCF 是在 Data Services 之后发布的。
internal class Context:DbContext {
public Context(String nameOrConnectionString) : base(nameOrConnectionString) { }
public DbSet<AlertQueue> Alerts { get; set; }
}
public class EntitySet:Abstract.EntitySet {
public EntitySet(String nameOrConnectionString) {
var context = new Context(nameOrConnectionString);
this.AlertQueueRepository = new Concrete.AlertQueueRepository(new Repository<AlertQueue>(context, context.Alerts));
}
}
public class AlertQueueRepository:EntityRepository<AlertQueue> {
public AlertQueueRepository(IEntityRepository<AlertQueue> entityRepository):base(entityRepository) { }
public IQueryable<AlertQueue> Pending {
get {
return (from alert in this.All
where alert.ReviewMoment == null
select alert);
}
}
}
EntityRepository
和 IEntityRepository
为 All
和其他 CRUD 函数提供通用方法。这是无法正常工作的 WCF 数据服务:
public class WcfDataService1:DataService<Domain.Concrete.AlertQueueRepository> {
public static void InitializeService(DataServiceConfiguration config) {
config.SetEntitySetAccessRule("All", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
Does WCF Data Services support working with Entity Framework Code First (4.1)? It seems like it's having a hard time understanding what to do with DbSet
or DbContext
. I suppose this shouldn't be too surprising since EFCF was released after Data Services.
internal class Context:DbContext {
public Context(String nameOrConnectionString) : base(nameOrConnectionString) { }
public DbSet<AlertQueue> Alerts { get; set; }
}
public class EntitySet:Abstract.EntitySet {
public EntitySet(String nameOrConnectionString) {
var context = new Context(nameOrConnectionString);
this.AlertQueueRepository = new Concrete.AlertQueueRepository(new Repository<AlertQueue>(context, context.Alerts));
}
}
public class AlertQueueRepository:EntityRepository<AlertQueue> {
public AlertQueueRepository(IEntityRepository<AlertQueue> entityRepository):base(entityRepository) { }
public IQueryable<AlertQueue> Pending {
get {
return (from alert in this.All
where alert.ReviewMoment == null
select alert);
}
}
}
EntityRepository
and IEntityRepository
provide generic methods for All
and other CRUD functions. This is the WCF Data Service that isn't working:
public class WcfDataService1:DataService<Domain.Concrete.AlertQueueRepository> {
public static void InitializeService(DataServiceConfiguration config) {
config.SetEntitySetAccessRule("All", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要安装 Microsoft WCF 数据服务 2011 CTP2。
以下是来自 ADO.NET 团队博客的一篇很好的分步文章:将 WCF 数据服务与 Entity Framework 4.1 和 Code First 结合使用
您将需要更改引用对于“System.Data.Services”和“System.Data.Services.Client”
安装CTP后,将其添加到“Microsoft.Data.Services”和“Microsoft.Data.Services.Client”,然后您将拥有新的V3协议版本(DataServiceProtocolVersion.V3)
添加如下构造函数以传入连接字符串或数据库姓名
You need to install Microsoft WCF Data Services 2011 CTP2.
Here is a good step by step article from the ADO.NET Team Blog: Using WCF Data Services with Entity Framework 4.1 and Code First
You will need to change the reference for “System.Data.Services” and “System.Data.Services.Client”
to “Microsoft.Data.Services” and “Microsoft.Data.Services.Client” after installing the CTP and then you will have a new V3 protocol version (DataServiceProtocolVersion.V3)
Add a constructor like below to pass in a connection string or database name
我刚刚做了这个非常简单的测试:
WCF Service:
Context:
它可以工作,但我担心它是只读的。 这是允许数据修改的解决方法。
I just made this very simple test:
WCF Service:
Context:
And it works but I'm afraid that it is read only. Here is a workaround to allow data modifications.