1

I did not specify the DbSet in my applicationdbcontext.

However, I am able to create order payments using the following method:

    public List<OrderPaymentDto> Create(CreateOrderPaymentDto createInput)
    {
        if (createInput == null) return null;


        var orderTotalPrice = this.orderRepository.GetSingleAsync(o => o.Id == createInput.OrderId).Await()?.Price;
        if (orderTotalPrice == null)
        {
            throw new NotFoundException($"An order with an id {createInput.OrderId} has not been found! ");
        }
        var list = new List<OrderPaymentDto>();


        if (createInput.OrderPaymentsTemplateGroupId != null && createInput.OrderPaymentsTemplateGroupId != 0)
        {
            var orderTemplates = this.orderPaymentsTemplateManager.GetAll(op => op.OrderPaymentsTemplateGroupId == createInput.OrderPaymentsTemplateGroupId);
            if (orderTemplates == null)
            {
                throw new NotFoundException("No order templates were found!");
            }

            //take the order repository total price


            foreach (var orderTemplate in orderTemplates)
            {
                OrderPayment orderPaymentToBeCreated = new OrderPayment
                {
                    Amount = ((orderTotalPrice.Value * orderTemplate.Amount) / 100),
                    OrderId = createInput.OrderId,
                    DueDate = DateTime.Now.AddDays(orderTemplate.PaymentPeriod),
                    PaymentType = orderTemplate.PaymentType,
                    Name = orderTemplate.Name

                };

                var addedOrderPayment = this.repository.AddAsync(orderPaymentToBeCreated).Await();
                list.Add(mapper.Map<OrderPaymentDto>(addedOrderPayment));
            }
        }
        
        else
        {
            OrderPayment orderPaymentToBeCreated = new OrderPayment
            {
                Amount = createInput.Amount,
                OrderId = createInput.OrderId,
                DueDate = DateTime.Now.AddDays(createInput.PaymentPeriod),
                PaymentType = createInput.PaymentType,
                Name = createInput.Name

            };
            var addedOrderPayment = this.repository.AddAsync(orderPaymentToBeCreated).Await();
            list.Add(mapper.Map<OrderPaymentDto>(addedOrderPayment));
        }

        this.notificationService.OnCreateEntity("OrderPayment", list);

        return list;
    }

the repository addasync method is this:

    public async Task<TEntity> AddAsync(TEntity entity)
    {
        ObjectCheck.EntityCheck(entity);
        await dbContext.Set<TEntity>().AddAsync(entity);
        await dbContext.SaveChangesAsync();

        return entity;

}

The table itself is created in PostGre, I am able to create entities.

What is the point of including them in the ApplicationDbContext?

The model itself has a reference to Order which has a dbset in the ApplicationDbContext. If entities are related can I just include one db set and not the rest?

My previous understanding of a DBSet is that it is used to have crud operations on the database. Now my understanding is challenged.

Can someone please clarify?

grozdeto
  • 1,201
  • 1
  • 13
  • 34
  • AFAIU without DbSets there will be no migrations for this tables . Those will be not included in context snapshot and, well, thats not to good because there could be troubles with deploying your actual db schema to other machines (environments) – Roman Kalinchuk Jun 25 '20 at 09:58
  • @Roman that's not true. OrderPayments are still included in the migrations and the snapshot. – grozdeto Jun 25 '20 at 10:01
  • @gozdeto thx, new info yo mee, stil, i think it is easier not to ommit part of declarations, while you still need some of them to be made – Roman Kalinchuk Jun 25 '20 at 10:03

1 Answers1

0

My colleague helped me find an answer.

https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-3.1

In this documentation in the section of Creating the DbContext example :

using ContosoUniversity.Models;
using Microsoft.EntityFrameworkCore;

namespace ContosoUniversity.Data
{
    public class SchoolContext : DbContext
    {
        public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
        {
        }

        public DbSet<Course> Courses { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Student> Students { get; set; }
    }
}

This code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.

You could've omitted the DbSet (Enrollment) and DbSet(Course) statements and it would work the same. The Entity Framework would include them implicitly because the Student entity references the Enrollment entity and the Enrollment entity references the Course entity.

grozdeto
  • 1,201
  • 1
  • 13
  • 34