简体   繁体   English

在实体框架核心中仅包含相关实体的Id

[英]Including only Id of related entity in entity framework core

I am currently developing an API with ASP.NET Core and Entity framework core with npgsql as database provider. 我目前正在使用ASP.NET核心和实体框架核心开发一个API,其中npgsql作为数据库提供者。 I have two Entities and they have a one to many relation. 我有两个实体,他们有一对多的关系。 The thing is that I only want to include the Id's of the child entity in the JSON result that the "Parent Controller" returns. 问题是我只想在“父控制器”返回的JSON结果中包含子实体的Id。

These are my entities: 这些是我的实体:

public class Meal {
    public int Id { get; set; }

    public string Title { get; set; }
    public string Description { get; set; }

    public string UserId { get; set; }
    public User User { get; set; }

    public List<Picture> Pictures { get; set; }

    public Meal () {
        this.Pictures = new List<Pictures>();
    }
}

public class Picture {
    public int Id { get; set; }

    public int MealId { get; set; }
    public Meal Meal { get; set; }

    public byte[] full { get; set; }
    public byte[] small { get; set; }
}

I am however not sure on how to achieve this. 但我不确定如何实现这一目标。 Yesterday I came across another SO question which suggested something like this: 昨天我遇到了另一个提出类似问题的SO问题:

public IActionResult Meals () {
    var meal = this.context.Meals
        .Include(m => m.Pictures.Select(p => p.Id))
        .First();

    return new JsonResult(meal);
}

This however throws an InvalidOperationException. 但是,这会抛出InvalidOperationException。 My DbContext is very basic, no onModelConfiguring because this code is following convention for as far as I know and it just has two DbSets of the corresponding types. 我的DbContext是非常基本的,没有onModelConfiguring,因为据我所知这个代码遵循约定,它只有两个相应类型的DbSet。 The foreign keys are also correct in the database and callling something like: 外键在数据库中也是正确的,并且调用类似于:

var pictures = dbContext.Pictures.Where(p => p.MealId == mealId).ToList();

Works as expected. 按预期工作。 I have only included the code which I thought was relevant. 我只包含了我认为相关的代码。 If more is needed I will include it, but I think this is completely my limited understanding of the queries. 如果需要更多,我将包括它,但我认为这完全是我对查询的有限理解。

Thank you for your time! 感谢您的时间!

You don't need to change your DB structure, one option is like so: 您不需要更改数据库结构,一个选项是这样的:

        var db = this.context;
        var result = (from meal in db.Meals
                      where meal.<whatever> == "123"
                      select new
                      {
                         Id = meal.Id,
                         Title = meal.Title,
                         Description = meal.Description,
                         //other required meal properties here.
                         PictureIds = meal.Pictures.Select(x => x.Id)
                      }).ToList();

You can do the same thing via lambda as well by using the "Select" method, Linq in such things seem more intuitive to me, however, to each his own... that's your choice. 你也可以通过lambda使用“选择”方法做同样的事情,Linq在这些事情上对我来说似乎更直观,但对于他自己......这是你的选择。

.Include(m => m.Pictures.Select(p => p.Id)) will not work you have to do .Include(m => m.Pictures.Select(p => p.Id))将不起作用你必须做

.Include(m => m.Pictures)

And this will get you an array of whole picture model for you (with all properties Id, MealId,..) and in your client side you can choose Id to work with.. 这将为您提供一系列全画面模型(所有属性Id,MealId,..),在您的客户端,您可以选择Id来使用..

There is probably a better answer out there, but this is how I fixed it. 可能有一个更好的答案,但这是我修复它的方式。 Including the entire Picture class was not an option since the binary data would also be included and I did not want to query the server for the data without using it since that is an expensive call to the database. 包括整个Picture类不是一个选项,因为二进制数据也将被包含在内并且我不想在不使用它的情况下向服务器查询数据,因为这是对数据库的昂贵调用。

So what I did is put the binary data in another class called PictureFile (need to think of a better name, but just File was obviously not an option). 所以我所做的是将二进制数据放在另一个名为PictureFile类中(需要考虑一个更好的名称,但只是File显然不是一个选项)。 The class PictureFile just has a reference to the corresponding picture and a byte array with the picture data. PictureFile类只引用相应的图片和带有图片数据的字节数组。 That way you can include the Pictures in a Meal without getting the actual files. 这样您就可以在不获取实际文件的情况下将Pictures包含在Meal The client can than later decide which pictures it needs and request them by PictureId . 客户端可以稍后决定它需要哪些图片并通过PictureId请求它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM