InvalidCastException:无法将数据库类型bpchar强制转换为Char

时间:2018-01-05 11:43:10

标签: c# entity-framework-core npgsql

我正在使用带有NpgSQL Nuget包的PgSQL数据库。 但是当它试图获取某些数据时会显示此错误

InvalidCastException: Can't cast database type bpchar to Char

我想这是因为数据库有一个char列,它就像一个PgSQL的bug,但是想知道你们中是否有人知道如何处理它。

p.s Leg定义id表是character,长度为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");
        });

谢谢。 enter image description here

1 个答案:

答案 0 :(得分:1)

正如错误消息所述,您应该将string用于字符列:

public string Leg { get; set; }

实体框架将确保在保存更改时,您分配给Leg的任何内容的长度均为1。