我剛剛遇到了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();
}
}
}
嘿,謝謝你的努力。我其實並沒有考慮過這個問題(主要是因爲我對此一無所知)。我正在檢查你給我的鏈接,我確信它至少會幫助我或者教新的東西。再次感謝! :) –
沒問題。如果您有其他問題,請告訴我。我是WCF數據服務的粉絲,我不認爲它應該得到應有的重視。 :) –
我其實有一個問題。我的計劃是創建一個從數據庫獲取數據的WCF服務,並將其提供給存儲在每個EntityType的某種「Dictionary/Collection」中的客戶端。客戶端應用程序將這些實體混淆在一起,然後將數據發送回服務,並將更改提交給數據庫** AND **將已更新/已更改的對象發送給* all *所連接的客戶端,並替換舊版本的本身在集合中。然後還有其他一些細節,例如鎖定客戶正在編輯的對象。這說 –