I just stumbled upon WCF today and started learning it. However, as soon as I tried to combine it with EntityFramework , it stopped working. I created an entity model for my dtcinvoicerdb database, turned off code generation, and wrote the Entity/ObjectContext classes myself. It is assumed that the service should get all Employees from the database.
Everything works fine, the project compiles and WcfTestClient opens, but when I try to call the GetEmployees() operation, I get the following exception:
Mapping and metadata information could not be found for EntityType 'DtcInvoicerDbModel.Employee'.
I know there is a lot of code here, but all this is pretty important, so bear with me.
image properties and object 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(); } } }