Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WCF
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 18-Oct-10 7:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
v2
Comments
Ryan Gamal at 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.
///

/// The associated lead ids.
[DataMember]
public ICollection 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 18 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100