2

I have this classes in Code First asp.net

public class Account
    {
        [Key]
        public int Id { get; set; }
        [Column(TypeName = "nvarchar(100)")]
        public string AccountTitle { get; set; }
        public Classification Classification { get; set; }
    }

    public class Classification 
    { 
        [Key]
        public int Id { get; set; }
        [Column(TypeName = "nvarchar(100)")]
        public string TitleClassification { get; set; }

        public ICollection<Account> Accounts { get; set; }
    }

    public class ClassificationDto
    {
        public int Id { get; set; }
        public string TitleClassification { get; set; }
    }

In my Db Context

public DbSet<Account> Accounts { get; set; }

AccountingManager.FindAll is this

public IQueryable<T> FindAll()
        {
            return context.Set<T>().AsNoTracking().AsQueryable();
        }

I am trying to get just the "Classification" which is just 3 but I am getting the "Account" that is associated with it too with this code:

[HttpGet]
        [Route("get-classification")]
        [Authorize]
        public async Task<IActionResult> GetAccountClassification()
        {
            List<ClassificationDto> classificationList = new List<ClassificationDto>();

            var accountingManager = new AccountingManager(context);

            var list = accountingManager.FindAll();

            classificationList = await list.Select(s => new ClassificationDto
            {
                Id = s.Classification.Id,
                TitleClassification = s.Classification.TitleClassification,
            }).ToListAsync();

            return StatusCode(StatusCodes.Status200OK, classificationList);
        }

This is how it is in my table enter image description here

1
  • You need to get all data from Classification table? Or you need only data that associated with Accounts? Commented Apr 2, 2021 at 10:36

1 Answer 1

2

If you want to get all rows from Classification table,

add public DbSet<Classification> Classifications { get; set;} property to your DbContext class. And then implement similar to FindAll() method:

return context.Classifications.AsNoTracking().ToList()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.