False value translated as null - Entity Framework

1

I have a boolean column in my database called HasEvent that has a constraint of not-null. When I try to add an item to the table via POST request and HasEvent is false, it is translated to null, and I get this error:

Npgsql.PostgresException (0x80004005): 23502: null value in column "HasEvent" violates not-null constraint

The data is showing up as it should. What could be happening?

PS: When I try to update an item already created and set its HasEvent value to false via PUT request, I get no error.

Here is my code:

Entity:

public class Period
{
    public long Id { get; set; }
    public DateTime InitialDate { get; set; }
    public DateTime? FinalDate { get; set; }
    public bool HasEvent { get; set; }
}

Controller:

[HttpPost]
public ActionResult<Period> Create(Period createPeriod)
{
    if (ModelState.IsValid)
    {
        try
        {
            var period = Service.Post(createPeriod);
            return Created("Created", period);
        }
        catch (Exception e)
        {
            return BadRequest(e);
        }
    }
    return BadRequest();
}

Method:

public Period Post(Period period)
{
    PeriodsRepository.Create(period);
    return period;
}

EDIT: Repository:

public interface PeriodsRepository<TEntity> : IDisposable where TEntity : class, new()
    {
        IQueryable<TEntity> SelectAll { get; }
        TEntity Select(long key);
        void Create(params TEntity[] obj);
        void Update(params TEntity[] obj);
        void Delete(params TEntity[] obj);
    }
c#
asp.net-core
entity-framework-core
crud

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0