2011-06-01 14 views
2

WCF Data Servicesは、Entity Frameworkコードファースト(4.1)での作業をサポートしていますか? DbSetまたはDbContextとどうすればよいか理解できないようです。私は、EFCFがData Servicesの後にリリースされたので、あまり驚くべきことではないと思います。WCF Data ServicesとEntity Frameworkのコードが最初にありますか?

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

私はこの非常に簡単なテストをした

public class WcfDataService1:DataService<Domain.Concrete.AlertQueueRepository> { 
    public static void InitializeService(DataServiceConfiguration config) { 
     config.SetEntitySetAccessRule("All", EntitySetRights.AllRead); 
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; 
    } 
} 
+0

EFv4.1またはカスタムリポジトリでWCFデータサービスを使用しますか?それはかなり大きな違いです。 –

+0

私は 'EntitySet'を持っている唯一の理由は、MVCのコントローラに単一のオブジェクトを提供でき、必要なエンティティセットを照会することができるということでした。 – Yuck

+0

しかし 'DbContext'を直接渡さないのはなぜですか? –

答えて

2

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」を変更する必要があります。 を「Microsoft.Data.Services」と「Microsoft.Data.Services.Client」にCTPをインストールした後、新しい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データサービスでありますそれは動作しますが、私はそれが読んでいるだけであることを恐れています。 Here is a workaroundを使用してデータを変更できます。

関連する問題