views:

36

answers:

1

I'm making a Silverlight 4 application with WCF RIA Services.

On the server side (the *.Web project), I have an Entity Model that's auto-generated from a SQL Server database. On the client side, I have the domain service and proxy objects that are generated by Visual Studio for use in Silverlight assemblies.

I want to add custom properties to the model (preferably on the server side). Say I have Contact, Company, and Address tables, which are linked by foreign keys (but not necessarily actual foreign key constraints). I want to add a property that will return a Contact's Company's Address object.

I have been attempting to do this by making a partial class to extend the Contact class, and adding a CompanyAddress { get; } property. But I have no idea what I need to do with the new property in order to make it propagate to the auto-generated code on the client side. Are there specific attributes I have to add to the property? Do I have to register it somewhere so that the code generator will know about it?

Does this have to be a Navigation Property or can it be something simpler?

And is this even the best way to do things, or should I give up on extending the server-side model and just do it on the client side? (If I do it on the client side, I face the problem of not having access to the context object inside the individual Entity-derived classes.)

A: 

I have never used Silverlight or RIA services but I guess it will be quite similar. When you create EF model and you have entities related by foreign key (there has to be relation), each entity related to other entity will contain something called navigation property. So in your scenario Contact should contain property named Company and Company shoud contain property called Address. You can isntruct EF to load those navigation properties by using Include on ObjectSet or by lazy loading (not good idea in WCF). Than if you send Contact by WCF to the client, Company and Address will be send as well.

Your approach has one big problem. Your property contains only getter - such property is not serialized.

Ladislav Mrnka
I don't currently have the ability to modify the existing database schema, so I can't depend on foreign key relations already existing in the generated .edmx file. I also must not manually modify the .edmx file, since the schema *may* change in the future, and my modifications would be overwritten when we regenerate the edmx file.
kpozin
Than add setter to your property and check if generated entity class is marked with DataContract attribute. If yes mark your property with DataMember attribute.
Ladislav Mrnka
Thank you for your help so far. I added a setter and marked the property with [DataMember]. This has no effect; the property isn't added to the Contact proxy class in Generated_Code\\*.Web.g.cs
kpozin
Check this article: http://devlicio.us/blogs/scott_seely/archive/2010/02/09/datacontract-partial-types-and-generated-code.aspx At the moment I don't understand why it doesn't work in your case.
Ladislav Mrnka