What am I doing wrong? wcf & entity-framework

I just stumbled upon WCF today and started learning it. However, as soon as I tried to combine it with EntityFramework , it stopped working. I created an entity model for my dtcinvoicerdb database, turned off code generation, and wrote the Entity/ObjectContext classes myself. It is assumed that the service should get all Employees from the database.

Everything works fine, the project compiles and WcfTestClient opens, but when I try to call the GetEmployees() operation, I get the following exception:

 Mapping and metadata information could not be found for EntityType 'DtcInvoicerDbModel.Employee'. 

I know there is a lot of code here, but all this is pretty important, so bear with me.

image properties and object 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(); } } } 
+4
source share
2 answers

I know this does not answer your question, but with your service, did you consider WCF data services instead? Your service returns your objects to your client — WCF Data Services can provide you and give your client the ability to use LINQ queries to filter and / or develop your results.

You can add a WCF data service project (InvoicerService.svc) to the project and configure the class of service as follows:

 public class InvoicerService : DataService<DtcInvoicerDbEntities> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead); config.SetEntitySetPageSize("*", 25); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } } 

You can then contact your employees with a GET request using the URL:

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

Again, not the answer to your question, but perhaps an alternative. Just thought I'd add.

Hope this helps!

+2
source

Ahh got it to work after doing some stackoverflow searching and comparing with the generated code.

Errors: I was missing the IsActive property, and I did not add Edm attributes to the class and its properties.

For those who are facing the same problem, be sure to specify the EdmScalarProperty attribute (or another type of property) for all your properties and remember to specify which property acts as an EntityKey (e.g. ID).

This is what my Employee class looks like and it works.

 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; } } } 
+2
source

Source: https://habr.com/ru/post/1368878/


All Articles