Orukele
Orukele

Reputation: 27

Getting data from a class that inherits from another class

I have been working on a library management web API with asp.net Core 3. I have a base class called LibraryAsset and another class called Book, the Book class inherits from the LibraryAsset class. I build my database with EF core CF workflow and a data table was created for the LibraryAsset that has a discriminator column.

I ran a test on the API to get all library assets, it returns the library asset however some properties are missing. These properties are in the Books class. How do I add these properties to the Library Asset when calling it with the API?

This is the Book.cs Class:

public class Book: LibraryAsset
    {
        public string ISBN { get; set; } 
        [Required]
        [StringLength(255)]
        public string Author { get; set; }
    }

while this is the LibraryAsset Class

public class LibraryAsset
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public int YearPublished { get; set; }
        public float Cost { get; set; }
    }

This the result I get when testing the API

{
    "id": 1,
    "title": "Children of Blood and Bone",
    "yearPublished": 2018,
    "cost": 8000
}

This is the Get Function for the API:

public async Task<IEnumerable<LibraryAsset>> GetAllAssets() {
      return await _context.LibraryAssets.ToListAsync();
}

Upvotes: 1

Views: 296

Answers (2)

Fei Han
Fei Han

Reputation: 27825

have a base class called LibraryAsset and another class called Book, the Book class inherits from the LibraryAsset class. I build my database with EF core CF workflow and a data table was created for the LibraryAsset that has a discriminator column.

ran a test on the API to get all library assets, it returns the library asset however some properties are missing.

If you'd like to get all books, you can try:

await _context.Books.ToListAsync();

If you'd like to get all assets from your database table LibraryAsset, you can try:

public async Task<IActionResult> GetAllAssets()
{    
    using (var command = _context.Database.GetDbConnection().CreateCommand())
    {
        command.CommandText = "SELECT Id, Title, YearPublished, Cost, Discriminator, ISBN, Author FROM dbo.LibraryAsset";
        _context.Database.OpenConnection();

        using (var myDataReader = command.ExecuteReader())
        {
            var dt = new DataTable();
            dt.Load(myDataReader);

            var allAssets = dt.AsEnumerable().Select(s => new
            {
                Id = s.Field<int>("Id"),
                Title = s.Field<string>("Title"),
                YearPublished = s.Field<int>("YearPublished"),
                Cost = s.Field<float>("Cost"),
                ISBN = s.Field<string>("ISBN"),
                Author = s.Field<string>("Author")
            }).ToList();

            return Ok(allAssets);
        }
    }
}

Upvotes: 1

dcg
dcg

Reputation: 4219

You could use JsonConvert.SerializeObject directly to return your JSON:

using Newtonsoft.Json;

public async Task<string> GetAllAssets() {
   return JsonConvert.SerializeObject(await _context.LibraryAssets.ToListAsync());
}

Alternatively you could change your signature to return a Task<JsonResult> and do return new JsonResult(await _context.LibraryAssets.ToListAsync());

Upvotes: 1

Related Questions