3

(I searched a lot and didn't find an answer yet, maybe it is not possible at all:/)

Is it possible to create entities in the EDM (from which I generated my DomainService) that are not mapped at all to any DB, just used instead between the DomainService and the Client? It might sound wierd for those who are entity framework pros:) My goal is to make it possible to return for example only a few properties of an entity instead of the whole entity. For example I need only the name and birthdate of a user, and I don't care about the other 10-20 properties. In this case a less complex entity would be perfect, is it possible to make these "lightweight" versions of the entities and use them in the same DomainService? Or any other suggestions for this kind of simple data querying scenario, using EF on top of the DB? (Or maybe just don't care about the bandwith in 2011 and retrieve full entities always? :)

Thanks, Bálint

1 Answers1

-1

You can load anonymous types by selecting sevaral properties of your entity like this,

 var studentEnrolments = from s in db.Students
                                  select new { s.FirstMidName,s.Enrollments};
        var studentEnrolmentsList= studentEnrolments.ToList();
        foreach (var studentEnrolment in studentEnrolmentsList)
        {
           //do something here 
        }

http://msdn.microsoft.com/en-us/library/bb738512.aspx

Edit..

 var studentEnrolments = from s in db.Students
                       select new Student{FirstMidName= s.FirstMidName, Enrollments=s.Enrollments};
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • I know but this is only possible afaik on the server side because WCF Ria Service doesn't allow to send back anonymous types. (only IQueryable etc..) So I should return named types (like IQueryable), like in this post: http://stackoverflow.com/questions/5176960/dynamic-query-with-wcf-ria-services But I don't know where to define this "xyz" named type that contains only some parts of the entity that EF mapped from the DB. – seekingtheoptimal Oct 12 '11 at 08:56
  • I found something that looks to me like the solution: http://forums.silverlight.net/t/182588.aspx I'll give it a try, I hope the client side will recognize it. – seekingtheoptimal Oct 12 '11 at 09:16
  • My problem is, that I'd like to make a new type that is not mapped from the databse by EF, just declared manually for the domainservice. And I don't know where to declare this new class. If I put it inside the DomainService class, it gets the wrong namespace when the client side version is generated. Intellisense recognizes it as an Entity after filling out my custom class metadata in DomainService.metadata.cs, but in the Web.g.cs file it searches for the entity in the Solutionname.Web.DomainService instead of just Solutionname.Web namespace. – seekingtheoptimal Oct 12 '11 at 15:26
  • I think if you want lite weight objects to use in WCF service it is better to implement DTO(Data transfer objects) layer in you solution to transfer data. In that layer you can have both lite weight objects and the normal objects wich are mapped to domain objects via some kind of mapper (AutoMapper). Then your lite weight object is in separate project. – Jayantha Lal Sirisena Oct 12 '11 at 17:46
  • Finally I made a "Presentation Model" (that's afaik the DTO layer you mentioned), thank you for your confirmation about my choice, I didn't really know that the soulution is called Pres. Model :) Can you edit your post to contain your last comment (with these links for ex? http://weblogs.asp.net/fredriknormen/archive/2009/11/28/wcf-ria-services-and-a-guide-to-use-dto-presentation-model.aspx and http://blogs.msdn.com/b/deepm/archive/2009/11/20/wcf-ria-services-presentation-model-explained.aspx)? Or may I just accept as an answer like this? I'm new around here, not sure what is the routine. – seekingtheoptimal Oct 20 '11 at 21:18