编程语言
首页 > 编程语言> > c# – 我做错了什么? wcf&entity-framework

c# – 我做错了什么? wcf&entity-framework

作者:互联网

我今天刚遇到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,entity-framework,wcf,poco,wcf-data-services
来源: https://codeday.me/bug/20190626/1294525.html