views:

353

answers:

2

I have a database I'd like to create an entity from, and then generate RESTful output.

My objective is to add a property to one of the tables once it becomes an entity. The data for that property would be one I'd come up with through calculations done on a few different fields in the table. From there, the code generator would create RESTful output like it normally does.

I have managed to be able to update the SSDL, CSDL, and the mapping sections of the edmx file along with using the SampleEdmxCodeGenerator as a custom tool. When I have all the sections in the edmx file filled out with my custom property, the svc fails because (I'm assuming) the property doesn't exist in the database. If I leave the property out of the SSDL, but put it in the client schema (CSDL) and the mapping section, I can't build my project.

I've modified the partial class and added to it, but the problem there is that I need to populate the methods on the creation time of the class, and I haven't been able to do that yet.

Am I headed in the right direction, or is this not possible? It seems like I should be able to do this with minimal effort, but I keep hitting walls.

+2  A: 

I think you're taking detours to get where you want. I haven't used either of these approaches (recently), so they might not do exactly what you're after, but you could try this:

  1. Create a partial class file right next to the .edmx model, which has the same name as your entity.
  2. In it, specify the property you want as a read-only property, that does the calculations on each get.
Tomas Lycken
yes, a partial class is the way to go... This property has nothing to do with the EDMX model if it's not going to be stored in the DB.
Thomas Levesque
How do I get the property to show up on the SVC output if the SVC output is based off the entity EDMX model?
georryan
More specifically, how do I get a read only property to show up in the SVC output (that won't affect the database), if the SVC is based off the EDMX model. This app won't save back to the database, so I just want to have data to read. I'd like an additional property that doesn't exist in the database to be shown in the output as well. I think I can get the partial classes to give me the data I need. How do I get that to the SVC?
georryan
Excuse my ignorance, but I don't know what you're talking about... What is "the SVC" (maybe spelling out the acronym is enough for me to get it ;) ), and how are you getting the rest of its "output"? (And what does the "output" consist of?)
Tomas Lycken
I'm creating an ADO.Net Data Service (*filename*.svc). That's where the svc comes in, it's the extension of the data service file. I'm telling it to look at one of my entities, and it automatically creates output based on that entity. It is in the form of RESTful web services and I can traverse the data that the service makes available to me through urls, as long as I have permission to view them.It sounds like partial classes can get me the data, but now I'm trying to get the output the way I want it. Right now, it only shows my entity fields, and I'd like to add one more.
georryan
A: 

Partial Classes and Partial methods were the first part of my answer. What I'm essentially trying to do I can't do. I can manipulate data that is returned by using partial methods and partial classes. I can plug the On*methodname*Changed() method to format the data how I'd like it to be shown, but that only gets me part way to my desired result.

What I would also like to do, is create a property c, which doesn't exist as a column in the database (and therefore does not exist in my entity), calculated from a couple different properties in the database (say a and b), and then add property c to the entity framework class. In doing this, I figured it would then get generated into the RESTful webservice output.

A problem that occurs comes from the need for the class to update any changes you make, and have it propagate back to the data source. I didn't care about that, because I want my property to be read only. From what I've gathered this isn't possible.

For reference, these two posts really helped: Adding custom property to Entity Framework class (I can only post one url currently, so here is the address to the other article)

social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b7a9e01d-c5c2-4478-8f01-00f7f6e0f75f

What I've decided to do, is to expose my entity as I've done so far, then consume the RESTful service that manipulates data and reformats it, and introduces needed properties. I'll turn the results into my own data object, and use that as a datasource to be exposed by yet another RESTful web service. I think this website gives a good example on how to expose a custom datasource.

mstecharchitect.blogspot.com/2008/12/surfacing-custom-data-source-in-adonet.html

If for some reason that is too slow, I suppose I could just make another table in my database that has a reworking of the data, and the calculated output in a format I'm looking for. The thing I want to avoid is having my resulting client having to do any of the data manipulation since it will be on some micro devices like palms, iphones, and blackberries.

Hope that helps anyone else with the same problem. It seems that is a shortfall in the current version of Data Services, but to some extent, I'm sure they'll be addressing it in later versions. Maybe T4 and .net 4.0 will be addressing it. I'm not sure.

georryan
I ended up using a LINQ to SQL solution to get my data, and then took the data, manipulated it to what I wanted, then put it into a List object composed of my own class. That List was iQueryable, and I used it as the data source for my RESTful output.It works like a charm.
georryan