实体框架子类未映射

时间:2015-09-16 20:57:29

标签: c# json entity-framework json.net

我正在尝试从我的DbContext创建一个方法,它允许我检索一组实体作为基本实体的子类。并且,我不希望子类被映射和/或存储在数据库中。

我正在尝试创建一个实体,它将我们的实体作为超类,可以针对第三方REST API进行序列化/反序列化。

例如:

我的实体

// 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; }
    }
}

所以我希望能够将Product对象检索为ShopifyProduct而不是Product。

这样的事情

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

我想避免的是将JsonPropertyAttribute放在原始实体上,因为实体将被锁定到一个特定的API。我可以看到有一个ShopifyProduct,一个EbayProduct等,JsonPropertyAttribute会开始发生冲突。

有人想过办法吗?

0 个答案:

没有答案