【发布时间】:2011-08-25 00:05:47
【问题描述】:
我今天刚遇到WCF 并开始学习它。但是,一旦我尝试将它与EntityFramework 结合使用,它就停止了工作。我为我的数据库dtcinvoicerdb 创建了一个实体模型,关闭了代码生成并自己编写了Entity/ObjectContext 类。该服务应该从数据库中获取所有Employees。
一切正常,项目编译并打开 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();
}
}
}
【问题讨论】:
标签: c# wcf entity-framework poco wcf-data-services