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();

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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; }
Yes, that's right - use another class (a DTO) with only the fields you need to send across the wire.
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
So have you created a new constructor in the EF class? You need to ensure a constructor without any parameters is in the class.
|
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

7 Comments

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();
You cannot pass the "u" parameter... that is why the exception.
hey i used Automapper and it simple made mapping fun for me :p
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 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.