我究竟做错了什么? wcf&实体框架

时间:2011-08-25 00:05:48

标签: 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();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

在对stackoverflow进行更多搜索并与生成的代码进行比较之后,让它工作了。

错误:我错过了一个属性IsActive,我没有将Edm属性添加到类及其属性中。

对于遇到相同问题的任何人,请记住在所有属性上设置EdmScalarProperty(或其他属性类型)属性,并且不要忘记设置哪个属性充当EntityKey(例如ID)

以下是我的Employee课程现在的样子,并且有效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;

[assembly: EdmSchemaAttribute()]
namespace DtcInvoicerDbModel
{
    [EdmEntityType(NamespaceName="DtcInvoicerDbModel", Name="Employee")]
    [DataContract(IsReference=true)]
    public class Employee
    {
        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = true, IsNullable = false)]
        public int ID { get; set; }

        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
        public bool IsActive { get; set; }

        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
        public string FirstName { get; set; }

        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
        public string LastName { get; set; }

        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
        public string Username { get; set; }

        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
        public string Password { get; set; }

        [DataMember]
        [EdmScalarProperty(EntityKeyProperty = false, IsNullable = true)]
        public DateTime? EmployeeSince { get; set; }
    }
}

答案 1 :(得分:2)

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

然后,您可以使用以下网址通过GET请求访问您的员工:

http://server:port/InvoicerService.svc/Employees?$filter=ID gt 0

同样,不是你的问题的答案,但也许是另一种选择。我以为我会添加它。

希望这有帮助!