将间隔列映射到NpgsqlTimeSpan

时间:2017-10-21 08:28:03

标签: c# .net .net-core entity-framework-core npgsql

在Entity Framework Core 2中的NPgSql中映射interval类型的表列的正确方法是什么?

以下是表格:

CREATE TABLE public.gap_set
(
  id bigint NOT NULL DEFAULT nextval('gap_set_id_seq'::regclass),
  "left" interval NOT NULL,
  "right" interval NOT NULL,
  avg_time_to_maturity double precision NOT NULL,
  avg_interval interval NOT NULL,
  name text NOT NULL,
  CONSTRAINT gap_set_pkey PRIMARY KEY (id)
)

我正在使用此代码:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using NpgsqlTypes;

namespace LarcNet.DAL.Entities
{
    [Table("maturity_sectors", Schema = "public")]
    public partial class GapSet
    {
        [Key, Column("id")]
        public long Id { get; set; }

        [Column("left", TypeName="interval")]
        public NpgsqlTimeSpan Left { get; set; }

        [Column("right", TypeName="interval")]
        public NpgsqlTimeSpan Right { get; set; }

        [Column("avg_time_to_maturity")]
        public double AverageTimeToMaturity { get; set; }

        [Column("avg_interval", TypeName="interval")]
        public NpgsqlTimeSpan AverageInterval { get; set; }

        [Column("name")]
        public string Name { get; set; }
    }
}

using System;
using Microsoft.EntityFrameworkCore;
using LarcNet.DAL.Entities;

namespace LarcNet.DAL.Context
{
        private readonly string m_ConnectionString;
        public DbSet<GapSet> GapSetSet { get; set; }
        public LarcContext(string connectionString)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString));
            }
            m_ConnectionString = connectionString;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder builder)
        {
            if (!string.IsNullOrEmpty(m_ConnectionString))
            {
                builder.UseNpgsql(m_ConnectionString);
            }
        }
    }
}
using System;
using System.Diagnostics;
using System.Linq;
using LarcNet.Contracts;
using LarcNet.Contracts.DataObjects.Communication;
using LarcNet.Contracts.DataObjects.Communication.Requests;
using LarcNet.Contracts.DataObjects.Communication.Responses;
using LarcNet.DAL.Context;
using LarcNet.DAL.Entities;

namespace LarcNet.ConsoleClient
{
    class MainClass
    {

        public static void Main(string[] args)
        {
            try
            {
                    using (var context = new LarcContext("server=127.0.0.1;userid=gadnio;database=mes_sample;Timeout=120;Command Timeout=120;Max Auto Prepare=1000;Auto Prepare Min Usages=1;Application Name=larc;"))
                    {
                        var list = context.GapSetSet.ToList();
                        foreach (var item in list)
                        {
                            Console.WriteLine("Gap Interval: Id={2} Left={0}, Right={1}", item.Left, item.Right, item.Id);
                        }
                    }
                }
        }
    }
}

它不起作用,而是抛出错误:

  

System.InvalidOperationException:读取时发生异常   数据库值。预期的类型是&#39; NpgsqlTypes.NpgsqlTimeSpan&#39;   但实际值是'System.Double&#39;的类型。 ---&GT;   System.InvalidCastException:无法将数据库类型float8转换为   NpgsqlTimeSpan at   Npgsql.NpgsqlDataReader.d__102 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ValueTaskAwaiter 1.GetResult()at   Npgsql.NpgsqlDataReader.d__98 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.ValueTask 1.get_Result()at   Npgsql.NpgsqlDataReader.GetFieldValue [T](Int32序数)at   lambda_method(Closure,DbDataReader)---内部异常结束   堆栈跟踪--- at   Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException [TValue](例外   异常,对象值,IPropertyBase属性)at   lambda_method(Closure,DbDataReader)at   Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader   dataReader)at   Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable 1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func 3操作,Func 3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable 1.Enumerator.MoveNext()   在   Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider&LT; _TrackEntities&GT; d__17 2.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.MoveNext()   在System.Collections.Generic.List 1.AddEnumerable(IEnumerable 1   System.Linq.Enumerable.ToList [TSource]的可枚举)(IEnumerable`1   来自LarcNet.ConsoleClient.MainClass.Main(String [] args)in   /data/wirk/repository/web_controller/larc-net2/LarcNet.ConsoleClient/Program.cs:line   26

我已经尝试将列映射到double,float和TimeSpan字段,但都无济于事。

0 个答案:

没有答案
相关问题