npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.52k stars 223 forks source link

Saving custom enum data type in pgsql in pgsql #2908

Open Anand-Chourasia opened 11 months ago

Anand-Chourasia commented 11 months ago

I am receiving an issue while saving the custom data type in pgsql using efcore pgsql (Npgsql.EntityFrameworkCore.PostgreSQL version-6.0.8) Error - "42804: column \"fhir_status\" is of type fhirvaccinesitestatus but expression is of type text.

I have registered the model in the db context but none of the solution is working.

roji commented 11 months ago

It sounds like you haven't fully followed the instruction for mapping enums in the docs. If you're sure you have, please submit a minimal code sample - a simple error message isn't enough to make us understand what you're doing.

Anand-Chourasia commented 11 months ago

Have registered this the enum postgres in the model builder.

modelBuilder.HasPostgresEnum("fhirvaccinesitestatus", new[] { "Active", "InActive" });

my entity class.

using AEG.DAL.Entities.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AEG.Constants.Enums;

namespace AEG.DAL.Entities.Masters
{
    /// <summary>
    /// Class ImmunizationVaccineSite.
    /// Implements the <see cref="MasterEntityBase" />
    /// </summary>
    /// <seealso cref="MasterEntityBase" />
    public class ImmunizationVaccineSite : MasterEntityFhirBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ImmunizationVaccineSite"/> class.
        /// </summary>
        public ImmunizationVaccineSite() : base() { }

        /// <summary>
        /// Gets or sets the fhir_lvl.
        /// </summary>
        [Required]
        [Column("fhir_lvl")]
        public int Level { get; set; }

        /// <summary>
        /// Gets or sets the fhir_internal_id.
        /// </summary>
        [Required]
        [Column("fhir_internal_id")]
        public long InternalId { get; set; }

        /// <summary>
        /// Gets or sets the fhir_status.
        /// </summary>
        [Required]
        [Column("fhir_status",TypeName = "fhirvaccinesitestatus")]
        public FhirVaccineSiteStatus FhirStatus { get; set; }

        /// <summary>
        /// Gets or sets the fhir_is_selectable.
        /// </summary>
        [Required]
        [Column("fhir_is_selectable")]
        public bool IsSelectable { get; set; }
    }
}
roji commented 11 months ago

What you've posted isn't a minimal code sample, it's a code fragment that doesn't allow us to see what your application looks like; a minimal code sample would e.g. be a small, runnable console program that can be executed, and produces the error.

Regardless, I'm guessing you may have missed the MapEnum() call - please read the docs again carefully.