entity-framework-4.1 - WCF 数据服务和 Entity Framework 代码优先?

标签 entity-framework-4.1 wcf-data-services

WCF 数据服务是否支持使用 Entity Framework 代码优先 (4.1)?似乎很难理解如何使用 DbSetDbContext。我想这应该不会太令人惊讶,因为 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);
        }
    }
}

EntityRepositoryIEntityRepositoryAll 和其他 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/

相关文章:

c# - 找不到 WCF 数据服务类型

silverlight - 在我的 ViewModel 中使用 DataServiceCollection 时如何实现 "Blendability"

.net - Entity Framework 外键到非主键字段

c# - 在一个 CMS/多个网站系统中管理数据库的最佳方法是什么

asp.net-mvc-3 - Entity Framework 4.1 代码优先的 SQL Server 2008 连接字符串

wcf - 使用 WCF 序列化 POCO 代理

.net - 我可以使用数据注释通过 Entity Framework 4.1 RC 执行级联删除吗?

json - WCF 数据服务 5.0 返回 POCO 的解决方法?

web-services - 每个线程具有不同参数的 JMeter 测试计划

c# - 异常消息是 'Value cannot be null. Parameter name: propertyResourceType'