0

I am trying to create a method off of my DbContext that will allow me to retrieve a collection of entities as a subclass of the base entity. And, I don't want the subclass to be mapped and/or stored in the database.

I am trying to do this to create an entity that has our entity as the superclass that can be serialized/de-serialized against a 3rd party REST API.

For example:

My Entity

// our legacy entity - EF code first
// stored in our database
public class Product
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }

    public string Title {get;set;}
}

// example 3rd party "wrapper" for JSON serialization
public class ShopifyProduct : Product
{
    [NotMapped]
    public string Name
    {
        get { return this.Title; }
        set { this.Title = value; }
    }
}

So I would like to be able to retrieve the Product objects as ShopifyProduct instead of Product.

So something like this

using (var context = new ProcessorDbContext())
{
    foreach (var product in context.Product<ProductShopify>())
    {
        ...
    }
}

What I want to avoid is putting the JsonPropertyAttribute on our original entities because then the entities would be locked to one specific API. I could see having a ShopifyProduct, an EbayProduct, etc and the JsonPropertyAttribute would start to clash.

Any anyone think of a way to do this?

3
  • Oh, and if it wasn't clear, I don't want to have to modify the underlying DB. Sep 16, 2015 at 21:06
  • I'm a little unclear on what exactly you want to do. If you're looking only to serialize the properties of the derived class and omit the base properties, see JProperty.WriteTo with collections in a JsonConverter.
    – dbc
    Sep 16, 2015 at 23:32
  • It's not really clear but it seems that you need (you have) a View Model different from the Persistency Layer Entities. In my experience, I can't solve all the possible issues inheriting from Persistency Layer Entities so usually the View Model entities are not inherited from the Persistency Layer Entities and I copy values with a mapper (i.e. Automapper).
    – bubi
    Sep 17, 2015 at 5:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.