c# - 我究竟做错了什么? wcf 和 Entity Framework

标签 c# wcf entity-framework poco wcf-data-services

我今天刚接触 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();
        }
    }
}

最佳答案

我知道这不能回答您的问题,但是对于您的服务,您是否考虑过使用 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

同样,这不是您问题的答案,而是一个替代方案。只是想我会添加它。

希望这对您有所帮助!

关于c# - 我究竟做错了什么? wcf 和 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183760/

相关文章:

c# - 选择多个 CSV 并批量写入数据库

.net - 本地未获取 "The maximum string content length quota (8192)"

c# - 与连接(桥接)表的一对多关系

c# - SQLCacheDependency 和 AppFabric 服务器

c# - 我们可以在.net窗口应用程序中运行javascript吗

android - 如何获取 OData v3 WCF 数据服务调用以使用 v3 协议(protocol)进行通信

wcf - 我应该在何时何地使用WCF

c# - EF6 表拆分与多个拆分的共享主键

sql-server - SQL Server 2005 在索引扫描之前进行全表排序

c# - 如何识别重叠日期范围的最大数量?