3

I am working on a WCF service to provide data to multiple mobile clients. Data model is Entity Framework 4.0. Schema is given below.

Schema

When i returnt he object of SysUser the result also contains the navigation properties and EntityKey and other EF related stuff. Is it possible that i get the pure object(only the database fields without the relationship etc).

Thanks Update the exception occures "Only parameterless constructors and initializers are supported in LINQ to Entities." on followign code:

return (from u in DataSource.SysUsers
                   where u.UserID == UserID
                   select new Player(u)
                   ).FirstOrDefault();
Romias
  • 13,783
  • 7
  • 56
  • 85
LojiSmith
  • 282
  • 3
  • 20

2 Answers2

3

You probably want to send DTOs across the wire rather than your EF objects.

You could use something like AutoMapper to populate your DTOs from the EF objects.

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
  • thanks Joe i think i got the correct answer, as i am new to EF so i thought there may be something build in in EF to get the DTOs. well for now i simply did the mapping thing in constructor of my own DTO. will use auto mapper later on for a bigger project if needed. – LojiSmith May 28 '13 at 12:21
  • one thing is confirm to me that to hide my business object and other stuff i need another class say player that contains only the information i have to send. public Player(SysUser User) { UserID = User.UserID; FirstName = User.FName; Email = User.Email; LastName = User.LName; Password = User.Password; ReceiveNewsletter = User.ReceiveNewsletter == null ? false : User.ReceiveNewsletter.Value; UserName = User.UserName; } – LojiSmith May 28 '13 at 12:23
  • Yes, that's right - use another class (a DTO) with only the fields you need to send across the wire. – Joe Ratzer May 28 '13 at 12:47
  • A problem, "Only parameterless constructors and initializers are supported in LINQ to Entities. exception is occurring" while passing the linq object to constructor. return ( from u in DataSource.SysUsers where u.UserID == UserID select new Player(u) ).FirstOrDefault(); – LojiSmith Kaleem 42 secs ago edit – LojiSmith May 28 '13 at 13:37
  • So have you created a new constructor in the EF class? You need to ensure a constructor without any parameters is in the class. – Joe Ratzer May 28 '13 at 13:49
  • yes there are 2 constructors one is parameterless and other is the one with object – LojiSmith May 28 '13 at 14:01
  • public Player() { } public Player(SysUser User) { UserID = User.UserID; FirstName = User.FName; Email = User.Email; LastName = User.LName; Password = User.Password; ReceiveNewsletter = User.ReceiveNewsletter == null ? false : User.ReceiveNewsletter.Value; } – LojiSmith May 28 '13 at 14:02
  • nopes well followign did the work return new PlayerDTO(( from u in DataSource.SysUsers where u.UserID == UserID select u ).FirstOrDefault()); – LojiSmith May 28 '13 at 14:06
2

I think if you remove the virtual keyword in your SysUser model for the navigation properties, those will not be loaded. Later, if you need to load this properties, you can do it manually as stated here: http://msdn.microsoft.com/en-us/data/jj574232

Now, if you want to make SysUser travel through a WCF service, it is not a good idea. First, your service's client will need a reference to your Models Project... and that doesn't feels right. If you don't reference your Models, you will get a proxy for it, that is more or less the same as Joe R explained about DTOs.

Here is a related answer: https://stackoverflow.com/a/7161377/7720

Community
  • 1
  • 1
Romias
  • 13,783
  • 7
  • 56
  • 85
  • A problem, "Only parameterless constructors and initializers are supported in LINQ to Entities. exception is occurring" while passing the linq object to constructor. return ( from u in DataSource.SysUsers where u.UserID == UserID select new Player(u) ).FirstOrDefault(); – LojiSmith May 28 '13 at 13:36
  • You cannot pass the "u" parameter... that is why the exception. – Romias May 28 '13 at 15:48
  • hey i used Automapper and it simple made mapping fun for me :p – LojiSmith May 29 '13 at 09:33
  • Why you don't feel right about returning entity !? your dto will be the exactly same object, instead of this; think code first and Ignore capabilities of the EF... – efaruk Aug 21 '15 at 06:28
  • @efaruk if you have multiple mobile clients using the service, it is better to not coupling with your EF models. Even if you don't have a service, in an MVC project for example, it is better to use ViewModels instead of your EF models in views. – Romias Aug 26 '15 at 16:41
  • If you need to couple few entity or extra information you are good to go, don't hesitate return your viewmodel. This is not the case I'm talking about. I'm saying you don't have to code a dto for an entity. You can use ignore capabilities of EF and you can return your EF entity (Actually can be more then your entity). In the real world, when you change your data model, your DTO will change every time (%99.9). So Why you are writing boiler plate code use same object. This is what I mean... – efaruk Oct 08 '15 at 11:17
  • @Romias can you gave a real world example. I'm pretty sure you can't. Because if your data changed %99.999 you will change your DTO too. So, I'm simply telling dont write a DTO just use your entity and ignore not required properties with [NotMapped], etc. And I'm telling you again ViewModel is not the case we are talking about... – efaruk Jan 25 '16 at 07:50