Search code examples
entity-frameworkwcf-data-services

How do I extend a dbcontext entity class?


I have exposed some EF5.0 entities generated from a database through at WCF data service.

The data service is consumed by a WPF-client which takes the entities (from the data service) and store them locally. I do this by creating a code-first entity database based on the WCF-entities:

public class LocalRaceContext : DbContext
{
    public LocalRaceContext() { }
    public LocalRaceContext(string connstr) : base(connstr) { }

    public DbSet<Participant> Participants { get; set; }
    .
    .
    . more ...
}

I want to extend the Participant with a new property (in the client-side model). I figured I could just do this with a partial class like so:

public partial class Participant
{
    public virtual List<Stamp> Stamps { get; set; }
}

This however does not work. Do I need some kind of attribute on the partial class?

I get the following error:

"The type 'RaceEntities+Participant' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject."

Edit:

@IronMan84: The original model (without the partial class) works because EF code-first takes care of the database and table creation. Actually it works perfectly fine and I am able to save the EF model in a local SQL CE file and retrieve the objects again as EF-classes later.

What I'm trying to achieve is to persist data from the data service locally but in a model which is somewhat extended. I've succeeded so far up until the extending part.

@Matt Whetton: It fails when I create a new instance of LocalRaceContext.

Edit2: I've tried to make an empty partial class (no properties). It still throws the same error.

Thanks in advance

Frederik


Solution

  • Nested classes are not yet supported by EF. Move the Participant class outside of RaceEntities.