WCF 数据服务是否支持使用 Entity Framework 代码优先 (4.1)?似乎很难理解如何使用 DbSet
或 DbContext
。我想这应该不会太令人惊讶,因为 EFCF 是在数据服务之后发布的。
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;
}
}
最佳答案
您需要安装Microsoft WCF Data Services 2011 CTP2 .
这是来自 ADO.NET 团队博客的一篇很好的分步文章:Using WCF Data Services with Entity Framework 4.1 and Code First
您需要更改“System.Data.Services”和“System.Data.Services.Client”的引用 安装CTP后将其更改为“Microsoft.Data.Services”和“Microsoft.Data.Services.Client”,然后您将拥有新的V3协议(protocol)版本(DataServiceProtocolVersion.V3)
添加如下所示的构造函数以传入连接字符串或数据库名称
public class HospitalContext : DbContext
{
public HospitalContext(string dbname) : base(dbname)
{
}
public DbSet<Patient> Patients { get; set; }
public DbSet<LabResult> LabResults { get; set; }
}
关于entity-framework-4.1 - WCF 数据服务和 Entity Framework 代码优先?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6205457/