WCF 数据服务和实体框架代码优先?

发布于 2024-11-11 17:23:33 字数 1520 浏览 3 评论 0原文

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

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;
    }
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-11-18 17:23:33

您需要安装 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)

添加如下构造函数以传入连接字符串或数据库姓名

public class HospitalContext : DbContext
{
    public HospitalContext(string dbname) : base(dbname)
    {
    }

    public DbSet<Patient> Patients { get; set; }
    public DbSet<LabResult> LabResults { get; set; }
}

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

public class HospitalContext : DbContext
{
    public HospitalContext(string dbname) : base(dbname)
    {
    }

    public DbSet<Patient> Patients { get; set; }
    public DbSet<LabResult> LabResults { get; set; }
}
燕归巢 2024-11-18 17:23:33

我刚刚做了这个非常简单的测试:

WCF Service:

public class WcfDataService : DataService<Context>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

Context:

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }

    public Context()
    {
        this.Configuration.ProxyCreationEnabled = false;
    }
}

[DataServiceKey("Id")]
public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

它可以工作,但我担心它是只读的。 这是允许数据修改的解决方法

I just made this very simple test:

WCF Service:

public class WcfDataService : DataService<Context>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

Context:

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }

    public Context()
    {
        this.Configuration.ProxyCreationEnabled = false;
    }
}

[DataServiceKey("Id")]
public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And it works but I'm afraid that it is read only. Here is a workaround to allow data modifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文