I just came across WCF today and started learning it. However, once I tried to combine it with the EntityFramework it stopped working. I created a entity model for my database dtcinvoicerdb, turned off code generation and wrote the Entity/ObjectContext classes myself. The service is supposed to fetch all the Employees from the database.
Everything works fine, the project compiles and the WcfTestClient opens up, but when I try to Invoke the GetEmployees() operation I get the following exception:
Mapping and metadata information could not be found for EntityType 'DtcInvoicerDbModel.Employee'.
I know there’s a lot of code below, but it’s all pretty basic so bear with me.
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();
}
}
}
I know this doesn’t answer your question, but with your service, have you considered using WCF Data Services instead? Your service is returning your entities to your client — WCF Data Services can provide that for you and also give your client the ability to use LINQ queries to filter and/or project your results.
You could add a WCF Data Service item (InvoicerService.svc) to your project and set up the service class like so:
You could then access your employees via a GET request using the URL:
Again, not the answer to your question, but perhaps an alternative. Just thought I’d add it.
Hope this helps!