【问题标题】:WCF Data Services and Entity Framework Code First?WCF 数据服务和实体框架代码优先?
【发布时间】:2011-06-01 17:44:53
【问题描述】:

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

【问题讨论】:

  • 您喜欢将 WCF 数据服务与 EFv4.1 或自定义存储库一起使用吗?这是相当大的区别。
  • 我拥有EntitySet 的唯一原因是我可以为 MVC 中的控制器提供单个对象,并且他们能够查询他们需要的任何实体集。
  • 但是为什么不直接传DbContext呢?
  • 顺便说一句,如果 WCF 数据服务确实提供了错误消息而不是其无用的“服务器在处理请求时遇到错误。有关更多详细信息,请参阅服务器日志。”
  • 他们提供错误但你必须告诉他们这样做:stackoverflow.com/questions/2312894/…

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


【解决方案1】:

你需要安装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协议版本(DataServiceProtocolVersion.V3)

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

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

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

【讨论】:

    【解决方案2】:

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

    WCF 服务:

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

    上下文:

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

    它可以工作,但我担心它是只读的。 Here is a workaround 允许修改数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多