Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Hi all,

I've created an entity model using Entity Framework 4, which I've exposed via a WCF Data Service. One of my entities needs to have properties defined that are not persisted to the database, but the Entity Model designer doesn't allow you to do this.

To get round this I've defined all my objects as POCO objects, which allows you to add non persisted properties to your objects, but not your model.

The issue I have is that, because these non persisted properties only exist in the objects themselves and not the model, they are not exposed via the WCF Data Service.

Is there any way to define properties in an entity model that are not persisted to the database?

Thanks in advance for any replies

Ryan
Posted

The answer is yes: You can add non-persistent properties to an EF model, but not through the designer.
The generated models are all partial classes, which means you can spread the full class implementation over several files. Let's say you have an Entity called Foo. You can add a file to the project called Foo.cs like this:


namespace MyNameSpace
{
  public partial class Foo
  {
    public string MyNonPersistantProperty {get; set; }
  } 
}


Note that both the type name and namespace name must match the one in the EF class. [edit] You should be able to mark these up as data contract properties. I only really answered the final part of your question, sorry!
 
Share this answer
 
v2
Comments
Ryan Gamal 25-Oct-10 5:55am    
Unfortunately, this still hasn't worked. I wonder if the issue is because I'm using a WCF Data service?

namespace Marshalls.CustomerActivity.ObjectModel
{
public partial class CustomerActivityReport
{
///
/// Gets or sets the associated lead ids.
///

/// <value>The associated lead ids.
[DataMember]
public ICollection<guid> AssociatedLeadIds { get; set; }
}
}

I've recompiled the project + updated the service reference but still no mention of AssociatedLeadIds in CustomerActivityReport entity on the client. FYI The CustomerActivityReport entity is persistence aware (not POCO) as there are other issues regarding using POCO entities with wcf data services
Microsoft have since released EF4.1 and a new version of WCF Data Services (currently in CTP) which allow you to expose non persisted properties via your data service.

To achieve this you need to define your entity model using the code first approach, which doesn't generate an EDMX file. You can use data annotations or the fluent API to specify that properties are not persisted. If you use the model first approach with the designer, then unfortunately, the data service will use the generated EDMX file to determine what properties to expose.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900