net core 2 EF是具有TPH继承的复制行

时间:2018-03-08 10:15:00

标签: c# inheritance ef-core-2.0 .net-core-2.0 tph

我目前正在开展net core 2应用程序。

我创建了一个" Goods"抽象类。

public abstract class Good : BaseEntity 
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Details { get; set; }
    public double Price { get; set; }
    public string IndexId { get; set; } 
    public string RefPintel { get; set; }
    public bool IsDiscountable { get; set; }
    public int ProductTypeId { get; set; }
    public bool IsEnabled { get; set; }
    public int DepartmentId { get; set; }
    public string Season { get; set; }
}

两个继承的产品和批次类。

public class Product : Good
{
    public string File { get; set; }
    public bool? IsDisplayedOnJCE { get; set; }
    public bool? IsBasicProduct { get; set; }
    public int? PintelSheetId { get; set; }


public class Batch : Good
{
    public ICollection<Product> Products { get; set; }

如您所见,批处理包含一系列产品。

一切正常,EF创造了一张桌子&#34;商品&#34;使用TPH继承模型。批处理中的产品具有BatchId(由EF创建的列),鉴别器列运行良好(即使我在代码中无法访问它)等等...

我的问题是,当我执行GetAll请求时,我会获得所有批处理产品两次。

 {
        "products": [
            {
                **"id": 85,**
                "title": "500 Pièces LEGO",
                "details": "Boîte de 500 pièces LEGO pour construire des trucs mega top",
                "refPintel": "0002",
                "file": "lego-500pcs.pdf",
                "providerId": 2,
                "productType": "Jouet"
            },
            {
                "id": 86,
                "title": "1000 Pièces LEGO",
                "details": "Boîte de 1000 pièces LEGO pour construire des trucs mega top",
                "refPintel": "0001",
                "file": "lego-1000pcs.pdf",
                "providerId": 3,
                "productType": "Jouet"
            }
        ],
        "id": 78,
        "details": "Lot super LEGO 1500 pièces ! ",
        "title": "Lot LEGO TOP WOWOW",
        "price": 139.99,
        "indexId": "J",
        "refPintel": "0004",
        "isDiscountable": true,
        "productTypeId": 1,
        "isEnabled": true,
        "departmentId": 1,
        "season": "2018",
        "isBatch": true,
        "createdBy": "mathieu",
        "updatedBy": "mathieu"
    },
    {
        "file": "lego-500pcs.pdf",
        "isDisplayedOnJCE": true,
        "isBasicProduct": true,
        "pintelSheetId": 0,
        "providerId": 2,
        "id": 85,
        "details": "Boîte de 500 pièces LEGO pour construire des trucs mega top",
        "title": "500 Pièces LEGO",
        "price": 50,
        "indexId": "G",
        "refPintel": "0002",
        "isDiscountable": false,
        "productTypeId": 1,
        "isEnabled": true,
        "departmentId": 1,
        "season": null,
        "isBatch": false,
        "createdBy": "mathieu",
        "updatedBy": "mathieu"
    },
    {
        "file": "lego-1000pcs.pdf",
        "isDisplayedOnJCE": true,
        "isBasicProduct": true,
        "pintelSheetId": 0,
        "providerId": 3,
        "id": 86,
        "details": "Boîte de 1000 pièces LEGO pour construire des trucs mega top",
        "title": "1000 Pièces LEGO",
        "price": 100,
        "indexId": "I",
        "refPintel": "0001",
        "isDiscountable": false,
        "productTypeId": 1,
        "isEnabled": true,
        "departmentId": 1,
        "season": null,
        "isBatch": false,
        "createdBy": "mathieu",
        "updatedBy": "mathieu"
    },

正如您所看到的,我有两个产品(ID 85和ID 86),这些产品包含在我的批次中并包含在我的&#34;商品&#34;查询。它们相同,它们应该在批次之外存在。

db screenshot

我设法不使用foreach

将其从我的商品查询中删除
  foreach (var item in query)
        {

            if (item is Batch)
            {
                var batch = item as Batch;
                query = query.Except(query.Where(p => batch.Products.Any(b => b.Id == p.Id)));
            }
        }

结果

 {
        "products": [
            {
                "id": 85,
                "title": "500 Pièces LEGO",
                "details": "Boîte de 500 pièces LEGO pour construire des trucs mega top",
                "refPintel": "0002",
                "file": "lego-500pcs.pdf",
                "providerId": 2,
                "productType": "Jouet"
            },
            {
                "id": 86,
                "title": "1000 Pièces LEGO",
                "details": "Boîte de 1000 pièces LEGO pour construire des trucs mega top",
                "refPintel": "0001",
                "file": "lego-1000pcs.pdf",
                "providerId": 3,
                "productType": "Jouet"
            }
        ],
        "id": 78,
        "details": "Lot super LEGO 1500 pièces ! ",
        "title": "Lot LEGO TOP WOWOW",
        "price": 139.99,
        "indexId": "J",
        "refPintel": "0004",
        "isDiscountable": true,
        "productTypeId": 1,
        "isEnabled": true,
        "departmentId": 1,
        "season": "2018",
        "isBatch": true,
        "createdBy": "mathieu",
        "updatedBy": "mathieu"
    },
    {
        "products": [
            {
                "id": 88,
                "title": "Puzzle 3d 850 pieces - poudlard la grande salle",
                "details": "Édifiez la célèbre École de Magie et de Sorcellerie avec Poudlard - Grande Salle, un puzzle 3D en mousse de 850 pièces. Vous y retrouverez plusieurs lieux connus du merveilleux monde d'Harry Potter tel que la grande salle, le bureau du directeur ainsi que plusieurs autres. Vous pouvez combiner ce puzzle avec Poudlard - Tour d'astronomie  (référence 04031480 vendue séparément) et obtenir un puzzle 3D de 1725 pièces. Livret d'instructions inclus. Assemblage à tenons et mortaises, produisant des structures et assemblages 3D des plus solides. Conception et reproductions d'illustrations de qualité supérieure. Pour des heures de divertissement et de plaisir en famille. Dim. : L.50 x l.38 x H.47 cm.",
                "refPintel": "0003",
                "file": "puzzle-Poudlards.pdf",
                "providerId": 2,
                "productType": "Jouet"
            }
        ],
        "id": 87,
        "details": "lot de produits harry potter",
        "title": "Lot Harry Potter",
        "price": 199.9,
        "indexId": "J",
        "refPintel": "0005",
        "isDiscountable": true,
        "productTypeId": 1,
        "isEnabled": true,
        "departmentId": 3,
        "season": "2018",
        "isBatch": true,
        "createdBy": "mathieu",
        "updatedBy": "mathieu"
    },

这是按预期工作的,我的产品85和86没有显示。但是你可能知道,表演正在受到这种影响。使用foreach,在1200行表格上获得结果大约需要10秒钟。没有,0.2秒。

我的问题是如何向我的用户发送我的所有商品,即产品和批次,而不会将属于批次的产品发送两次(仅在批次内,而不是批次之外的产品),没有&# 34;滤波&#34;我对foreach的询问。

我尝试过其他一些事情,比如玩我的模型制作者等等......没有成功......

也许我在那里遗漏了一些明显的东西,因为我对此很陌生,但我目前仍然遇到了这个性能问题。

如果您需要任何澄清,详细信息或其他任何内容,请告诉我。

感谢您阅读。

编辑:添加了一个数据库屏幕截图,以显示它是否可以帮助解决问题。

编辑2:添加了foreach的输出。

编辑3:稍微编辑了一下这个问题。

0 个答案:

没有答案