Home > other >  .Include() method in APIController with one-to-many relations?
.Include() method in APIController with one-to-many relations?

Time:03-15

One display should have many media(images, videos) in my project. I'm using Entity Framework Core to build my database, and CRUD with my API Controller.

I designed my Model classes as such:

[Table("Displays")]
public class Display : ConcurrencyCheck
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "{0} skal udfyldes!")]
    [Display(Name = "Display navn")]
    public string Name { get; set; }

    [Display(Name = "Display tændt")]
    public bool IsOn { get; set; }

    [Display(Name = "Display beskrivelse")]
    public string Description { get; set; }

    public IEnumerable<Video> Videos { get; set; }

    public IEnumerable<Image> Images { get; set; }
}

public abstract class Media : ConcurrencyCheck
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "{0} skal udfyldes!")]
    [Display(Name = "Medie navn")]
    public string Name { get; set; }

    [Display(Name = "Medie beskrivelse")]
    public string Description { get; set; }

    [Display(Name = "Medie filtype")]
    public string FileType { get; set; }

    [Required(ErrorMessage = "{0} skal udfyldes!")]
    [Display(Name = "Medie filsti")]
    public string FilePath { get; set; }

    public int? DisplayId { get; set; }

    [ForeignKey("DisplayId")]
    public Display Display { get; set; }
}

[Table("Videos")]
public class Video : Media
{
    [Display(Name = "Frames")]
    public int Frames { get; set; }

    [Display(Name = "Filsti på undetekster")]
    public string SubtitlesPath { get; set; }

    [Display(Name = "Filsti på thumbnail")]
    public string ThumbnailPath { get; set; }
}

[Table("Images")]
public class Image : Media
{
}

public abstract class ConcurrencyCheck
{
    [Timestamp]
    public byte[] RowVersion { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
    public DateTime CreatedDate { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
    public DateTime? UpdatedDate { get; set; }
}

And my API controllers are scaffolded with 'API Controller with actions, using Entity Framework'.

Currently in the api-controller 'DisplaysController', I don't see the .Include(x => x.Media), though I read somewhere it's used? One display should have many media(images, videos).

Should I include it somehow somewhere, or does it do that automatically by the models I have?

CodePudding user response:

scaffolded with API Controller with actions, using Entity Framework just provide the most basic five database operation methods -- select , select by id, update, add and delete. Its purpose is to help users build basic crud faster and more easily. You can add your own code on the default code template.

The default code template

enter image description here

Add your own code

enter image description here

  • Related