开发者

Silverlight / .NET RIA Services - Exposing a custom property to the client

I have a table in my database called "Task". Task has the following fields: - ID - Description - AssignedUserID - TaskTypeID

I am accessing this table through a class that was created automatically after I used an ADO.NET Entity Data Model. I can load and show the fields mentioned above in a DataGrid in my Silverlight application. However, AssignedUserID and TaskTypeID are not very descriptive. So I decided to create a stored procedure that gets the tasks and the user and task type names through their respective lookup tables. This is where the problem lies.

I want to create some custom properties in the automatically g开发者_如何学编程enerated "Task" class. The custom properties would be named "AssignedUserName" and "TaskType". I then want to make these properties available to my Silverlight client. However, I cannot seem to figure out how to get them exposed to my Silverlight client.

Can someone help? Thank you


If your EDM is in the same project as the DomainService you can do this:

create a partial class on the Entity type, and add your calculated property in there.

name the file **.shared.cs

it will then be auto-shared with the client/Silverlight code.


Edit: I was assuming that you could do this calculation in app logic rather than use an sp, which seems more straightforward to me.

If you do use an SP, you'll need to use the Function Import feature in the designer to map the SP to a function in the EDM. This function can then return entities, with properties mapped however you like.

An easier way would be to just use the object model: Have Task.AssignedUser and Task.TaskType objects off of your Task class. Map these to lookup tables in your db. This will work out-of-the box (assuming the Id's are FK's to those lookup tables).

So, a couple options:

  1. use app-logic--properties in a partial class to return the descriptions
  2. use the object model driven by FKs to lookup tables, then just access Task.AssignedUser.Name or Task.TaskType.Description
  3. use a function import to access the SP and map the returned values to entity properties

1 or 2 being the best options IMHO.


Another approach might be to update your EF model to include the lookup tables, add Associations between the tables, add [Include]s in the (auto-gen'd) metadata class and let EF and RIA do it for you. Maybe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜