InvalidCastException: Can't cast database type bpchar to Char

c# entity-framework-core npgsql

Question

I am using a PgSQL database with NpgSQL Nuget package. But when it tries to get some data is shows this error

InvalidCastException: Can't cast database type bpchar to Char

I guess that happens because database has a char column, and its like a bug of PgSQL, but wanted to know if anyone of you guys have any idea of how to handle that.

p.s Leg definition id table is character with length of 1

DataController.cs

[HttpGet]
public IActionResult Get()
{
    var model = _repository.Get(20);
    return Ok(model);
}

Repository.cs

public IEnumerable<VXmlCdr> Get(int howMany)
{
    return _context.VXmlCdr.Take(howMany);
}

Startup.cs

services.AddEntityFrameworkNpgsql()
        .AddDbContext<fusionpbxContext>(options => options.UseNpgsql(connectionString));

VXmlCdr.cs

public partial class VXmlCdr
{
    public Guid Uuid { get; set; }
    public Guid? DomainUuid { get; set; }
    public Guid? ExtensionUuid { get; set; }
    public string DomainName { get; set; }
    public string Accountcode { get; set; }
    public string Direction { get; set; }
    public string DefaultLanguage { get; set; }
    public string Context { get; set; }
    public string Xml { get; set; }
    public string Json { get; set; }
    public string CallerIdName { get; set; }
    public string CallerIdNumber { get; set; }
    public string SourceNumber { get; set; }
    public string DestinationNumber { get; set; }
    public decimal? StartEpoch { get; set; }
    public DateTime? StartStamp { get; set; }
    public DateTime? AnswerStamp { get; set; }
    public decimal? AnswerEpoch { get; set; }
    public decimal? EndEpoch { get; set; }
    public string EndStamp { get; set; }
    public decimal? Duration { get; set; }
    public decimal? Mduration { get; set; }
    public decimal? Billsec { get; set; }
    public decimal? Billmsec { get; set; }
    public string BridgeUuid { get; set; }
    public string ReadCodec { get; set; }
    public string ReadRate { get; set; }
    public string WriteCodec { get; set; }
    public string WriteRate { get; set; }
    public string RemoteMediaIp { get; set; }
    public string NetworkAddr { get; set; }
    public string RecordingFile { get; set; }
    public char? Leg { get; set; }
    public decimal? PddMs { get; set; }
    public decimal? RtpAudioInMos { get; set; }
    public string LastApp { get; set; }
    public string LastArg { get; set; }
    public string CcSide { get; set; }
    public Guid? CcMemberUuid { get; set; }
    public string CcQueueJoinedEpoch { get; set; }
    public string CcQueue { get; set; }
    public Guid? CcMemberSessionUuid { get; set; }
    public string CcAgent { get; set; }
    public string CcAgentType { get; set; }
    public decimal? Waitsec { get; set; }
    public string ConferenceName { get; set; }
    public Guid? ConferenceUuid { get; set; }
    public string ConferenceMemberId { get; set; }
    public string DigitsDialed { get; set; }
    public string PinNumber { get; set; }
    public string HangupCause { get; set; }
    public decimal? HangupCauseQ850 { get; set; }
    public string SipHangupDisposition { get; set; }
}

Context.cs

    public virtual DbSet<VXmlCdr> VXmlCdr { get; set; }

        modelBuilder.Entity<VXmlCdr>(entity =>
        {
            entity.HasKey(e => e.Uuid);

            entity.ToTable("v_xml_cdr");

            entity.Property(e => e.Uuid)
                .HasColumnName("uuid")
                .ValueGeneratedNever();

            entity.Property(e => e.Accountcode).HasColumnName("accountcode");

            entity.Property(e => e.AnswerEpoch).HasColumnName("answer_epoch");

            entity.Property(e => e.AnswerStamp).HasColumnName("answer_stamp");

            entity.Property(e => e.Billmsec).HasColumnName("billmsec");

            entity.Property(e => e.Billsec).HasColumnName("billsec");

            entity.Property(e => e.BridgeUuid).HasColumnName("bridge_uuid");

            entity.Property(e => e.CallerIdName).HasColumnName("caller_id_name");

            entity.Property(e => e.CallerIdNumber).HasColumnName("caller_id_number");

            entity.Property(e => e.CcAgent).HasColumnName("cc_agent");

            entity.Property(e => e.CcAgentType).HasColumnName("cc_agent_type");

            entity.Property(e => e.CcMemberSessionUuid).HasColumnName("cc_member_session_uuid");

            entity.Property(e => e.CcMemberUuid).HasColumnName("cc_member_uuid");

            entity.Property(e => e.CcQueue).HasColumnName("cc_queue");

            entity.Property(e => e.CcQueueJoinedEpoch).HasColumnName("cc_queue_joined_epoch");

            entity.Property(e => e.CcSide).HasColumnName("cc_side");

            entity.Property(e => e.ConferenceMemberId).HasColumnName("conference_member_id");

            entity.Property(e => e.ConferenceName).HasColumnName("conference_name");

            entity.Property(e => e.ConferenceUuid).HasColumnName("conference_uuid");

            entity.Property(e => e.Context).HasColumnName("context");

            entity.Property(e => e.DefaultLanguage).HasColumnName("default_language");

            entity.Property(e => e.DestinationNumber).HasColumnName("destination_number");

            entity.Property(e => e.DigitsDialed).HasColumnName("digits_dialed");

            entity.Property(e => e.Direction).HasColumnName("direction");

            entity.Property(e => e.DomainName).HasColumnName("domain_name");

            entity.Property(e => e.DomainUuid).HasColumnName("domain_uuid");

            entity.Property(e => e.Duration).HasColumnName("duration");

            entity.Property(e => e.EndEpoch).HasColumnName("end_epoch");

            entity.Property(e => e.EndStamp).HasColumnName("end_stamp");

            entity.Property(e => e.ExtensionUuid).HasColumnName("extension_uuid");

            entity.Property(e => e.HangupCause).HasColumnName("hangup_cause");

            entity.Property(e => e.HangupCauseQ850).HasColumnName("hangup_cause_q850");

            entity.Property(e => e.Json)
                .HasColumnName("json")
                .HasColumnType("jsonb");

            entity.Property(e => e.LastApp).HasColumnName("last_app");

            entity.Property(e => e.LastArg).HasColumnName("last_arg");

            entity.Property(e => e.Leg)
                .HasColumnName("leg")
                .HasColumnType("char(1)");

            entity.Property(e => e.Mduration).HasColumnName("mduration");

            entity.Property(e => e.NetworkAddr).HasColumnName("network_addr");

            entity.Property(e => e.PddMs).HasColumnName("pdd_ms");

            entity.Property(e => e.PinNumber).HasColumnName("pin_number");

            entity.Property(e => e.ReadCodec).HasColumnName("read_codec");

            entity.Property(e => e.ReadRate).HasColumnName("read_rate");

            entity.Property(e => e.RecordingFile).HasColumnName("recording_file");

            entity.Property(e => e.RemoteMediaIp).HasColumnName("remote_media_ip");

            entity.Property(e => e.RtpAudioInMos).HasColumnName("rtp_audio_in_mos");

            entity.Property(e => e.SipHangupDisposition).HasColumnName("sip_hangup_disposition");

            entity.Property(e => e.SourceNumber).HasColumnName("source_number");

            entity.Property(e => e.StartEpoch).HasColumnName("start_epoch");

            entity.Property(e => e.StartStamp).HasColumnName("start_stamp");

            entity.Property(e => e.Waitsec).HasColumnName("waitsec");

            entity.Property(e => e.WriteCodec).HasColumnName("write_codec");

            entity.Property(e => e.WriteRate).HasColumnName("write_rate");

            entity.Property(e => e.Xml).HasColumnName("xml");
        });

Thank you. enter image description here

1
0
1/5/2018 12:02:08 PM

Accepted Answer

As the error message states, you should use string for character columns:

public string Leg { get; set; }

Entity Framework will ensure that whatever you assign to Leg has a length of 1 when saving changes.

1
1/5/2018 12:17:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow