3

My question: I would like to add a simple typed property (e.g. string, int, double) to my Entity Framework entity (in my ASP.NET project) that is not associated with any field in the database table that corresponds to the entity. I would like to be able to set the value of this property in my ASP.NET project and have the contents of the property auto-magically sent over to the Silverlight client gets the entities via RIA Services.

How do I do this?

Note: If this makes it easier, for my particular instance, I don't need to save the entities back to the database. Also, it would be great if this worked for views as well as tables.

My setup: I am using Silverlight 4, Entity Framework 4, and RIA Services. It is set up in the normal fashion: Silverlight client application and an ASP.NET server application; I am generating my EF model from the database. RIA services is generating the entities and database access methods on the Silverlight client.

My example:

Database: Customer table

  • CustomerID
  • CustomerName

EF generated entity (ASP.NET server-side): Customer class

  • Public Property CustomerID as Integer
  • Public Property CustomerName as String

I'd like to add a property to the Customer entity that is not associated with the database:

  • Public Property UnicornColor as string

In the my domain service (ASP.NET server side), I'll fill in the new property myself:

Public Function GetCustomers() As IQueryable(Of Customer)
    Dim customers as IQueryable(of Customer) = Me.ObjectContext.Customers
    For each c as Customer in customers
       c.UnicornColor = "Green"
    Next
    return customers
End Function

On the client side, I'd like this new property and its values to be there when I run my query:

Public Sub LoadCustomers()
    myContext.Load(myContext.GetCustomersQuery, AddressOf CustomersLoaded, Nothing)
End Sub

Public Sub CustomersLoaded(ByVal loadOp as LoadOperation(Of Customer))
    Dim customers as IEnumerable(Of Customer) = loadOp.Entities
    For Each c as Customer in customers
        dim color as string = c.UnicornColor
    Next
End Sub
2
  • I am generating my EF model from an existing database. Commented Jun 21, 2011 at 21:15
  • To be more clear, I am using the ADO.NET Entity Data Model template and the Entity Data Model Wizard to generate my model and entities, as described here in the "Displaying Data" section: msdn.microsoft.com/en-us/library/ff713719%28v=VS.91%29.aspx I am also creating my domain service as described in the walkthrough. Commented Jun 21, 2011 at 22:58

1 Answer 1

3

Use T4s to generate partial classes for entities (e.g. the POCOs T4s in the Visual Studio extension gallery) then add a file e.g. MyEntity.Part2.cs with the same partial class as the generated file, but that contains the new properties.

For more info Google partial classes C#.

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

3 Comments

Does this solution allow RIA Services to create the new properties on the client side and also move the values for these properties from the server to the client during a query? ---------- I currently have EF generate my entities from the database. I tried adding a partial class to Customers and a new property (in the ASP.NET server side project). But, RIA Services does NOT create the property for me on my client Customer entity.
Did you put a DataMember attribute on the new property?
Adding the DataMember attribute to the new property worked. Thanks.

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.