2011-08-25 63 views
4

我剛剛遇到了WCF,並開始學習它。但是,一旦我嘗試將它與EntityFramework結合,它就停止工作。我爲我的數據庫dtcinvoicerdb創建了一個實體模型,關閉了代碼生成並自己編寫了Entity/ObjectContext類。該服務應該從數據庫中獲取所有Employees我在做什麼錯? wcf&entity-framework

一切工作正常,項目編制和WcfTestClient打開了,但是當我嘗試調用GetEmployees()操作,我得到以下異常:

Mapping and metadata information could not be found for EntityType 'DtcInvoicerDbModel.Employee'. 

我知道有很多下面的代碼,但它的所有非常基本的,所以忍受着我。

entity image and model properties http://img716.imageshack.us/img716/1397/wcf.png

/Entities/DtcInvoicerDbContext.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Data.Objects; 

using DtcInvoicerDbModel; 

namespace DtcInvoicerServiceLibrary 
{ 
    public class DtcInvoicerDbContext:ObjectContext 
    { 
     public DtcInvoicerDbContext():base("name=DtcInvoicerDbEntities", "DtcInvoicerDbEntities") 
     { 
     } 

     #region public ObjectSet<Employee> Employees; 
     private ObjectSet<Employee> _Employees; 
     public ObjectSet<Employee> Employees 
     { 
      get 
      { 
       return (_Employees == null) ? (_Employees = base.CreateObjectSet<Employee>("Employees")) : _Employees; 
      } 
     } 
     #endregion 
    } 
} 

/Entities/Employee.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Data.Objects.DataClasses; 
using System.Runtime.Serialization; 

namespace DtcInvoicerDbModel 
{ 
    [DataContract] 
    public class Employee 
    { 
     [DataMember] 
     public int ID { get; set; } 
     [DataMember] 
     public string FirstName { get; set; } 
     [DataMember] 
     public string LastName { get; set; } 
     [DataMember] 
     public string Username { get; set; } 
     [DataMember] 
     public string Password { get; set; } 
     [DataMember] 
     public DateTime EmployeeSince { get; set; } 
    } 
} 

/IDtcInvoicerServicer.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.ServiceModel; 

using DtcInvoicerDbModel; 

namespace DtcInvoicerServiceLibrary 
{ 
    [ServiceContract] 
    public interface IDtcInvoicerService 
    { 
     [OperationContract] 
     List<Employee> GetEmployees(); 
    } 
} 

/DtcInvoicerService.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.ServiceModel; 

using DtcInvoicerDbModel; 

namespace DtcInvoicerServiceLibrary 
{ 
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, IncludeExceptionDetailInFaults=true)] 
    public class DtcInvoicerService:IDtcInvoicerService 
    { 
     private DtcInvoicerDbContext db = new DtcInvoicerDbContext(); 

     public List<Employee> GetEmployees() 
     { 
      return db.Employees.Where(x => x.ID > 0).ToList(); 
     } 
    } 
} 

回答

2

我知道這並不能回答你的問題,但你的服務,你有沒有考慮過使用WCF Data Services呢?您的服務正在將您的實體返回給您的客戶端 - WCF數據服務可以爲您提供這些服務,同時也爲您的客戶提供使用LINQ查詢來過濾和/或投影結果的能力。

您可以添加一個WCF數據服務項目(InvoicerService.svc)到項目,併成立了像這樣的服務類:

public class InvoicerService : DataService<DtcInvoicerDbEntities> 
{ 
    public static void InitializeService(DataServiceConfiguration config) 
    { 
     config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead); 

     config.SetEntitySetPageSize("*", 25); 
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; 
    } 
} 

然後,您可以通過使用URL GET請求訪問你的員工:

http://server:port/InvoicerService.svc/Employees?$filter=ID gt 0 

再次,不是你的問題的答案,但也許是一種選擇。只是想我會添加它。

希望這會有所幫助!

+0

嘿,謝謝你的努力。我其實並沒有考慮過這個問題(主要是因爲我對此一無所知)。我正在檢查你給我的鏈接,我確信它至少會幫助我或者教新的東西。再次感謝! :) –

+1

沒問題。如果您有其他問題,請告訴我。我是WCF數據服務的粉絲,我不認爲它應該得到應有的重視。 :) –

+0

我其實有一個問題。我的計劃是創建一個從數據庫獲取數據的WCF服務,並將其提供給存儲在每個EntityType的某種「Dictionary/Collection」中的客戶端。客戶端應用程序將這些實體混淆在一起,然後將數據發送回服務,並將更改提交給數據庫** AND **將已更新/已更改的對象發送給* all *所連接的客戶端,並替換舊版本的本身在集合中。然後還有其他一些細節,例如鎖定客戶正在編輯的對象。這說 –

2

稀釋了它經過一些搜索的計算器,用生成的代碼比較工作。

錯誤:我缺少一個屬性IsActive,我沒加EDM屬性的類和它的屬性。

對於遇到同樣問題的任何人,請記住在所有屬性上設置EdmScalarProperty(或其他屬性類型)屬性,並且不要忘記設置哪個屬性作爲EntityKey(例如ID)。

下面是我的Employee類現在的樣子,它的工作原理。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Data.Objects.DataClasses; 
using System.Runtime.Serialization; 

[assembly: EdmSchemaAttribute()] 
namespace DtcInvoicerDbModel 
{ 
    [EdmEntityType(NamespaceName="DtcInvoicerDbModel", Name="Employee")] 
    [DataContract(IsReference=true)] 
    public class Employee 
    { 
     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = true, IsNullable = false)] 
     public int ID { get; set; } 

     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)] 
     public bool IsActive { get; set; } 

     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)] 
     public string FirstName { get; set; } 

     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)] 
     public string LastName { get; set; } 

     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)] 
     public string Username { get; set; } 

     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)] 
     public string Password { get; set; } 

     [DataMember] 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = true)] 
     public DateTime? EmployeeSince { get; set; } 
    } 
} 
+0

很高興看到你得到它的工作......但我想我會採取戴夫方法:) – War