2016-10-05 2 views
0

클래스가 Sample이고 속성 중 하나가 enum 인 TargetType입니다. 일치하는 열거 형 targettypes과 함께 PostgreSQL 데이터베이스에 정의 된 해당 테이블 samples이 있습니다.Dapper.FastCRUD를 사용하여 C# enum을 PostgreSQL enum으로 매핑하는 방법은 무엇입니까?

Dapper.FastCRUD를 사용하면 테이블에서 레코드를 성공적으로 검색 할 수 있습니다. Dapper.FastCRUD의 창조자 - - MoonStorm DB-CLR 형식 변환이 단정에 의해 처리되는 것을 명확히 :

Npgsql.PostgresException (0x80004005): 42804: column "target_type" is of type targettype but expression is of type integer 

편집 1 : 그러나, 나는 삽입하는 동안 오류가 발생합니다. 이제 질문은 다음과 같습니다.

C# enum TargetType을 PostgreSQL에 매핑하려면 Dapper에게 어떻게 말합니까? ENUM TYPE targettype?

열거 형은 다음과 같이 정의된다

public enum TargetType 
{ 
    [NpgsqlTypes.PgName("Unknown")] 
    UNKNOWN = 0, 

    [NpgsqlTypes.PgName("Animal")] 
    ANIMAL = 1, 

    [NpgsqlTypes.PgName("Car")] 
    CAR = 2, 

    [NpgsqlTypes.PgName("Truck")] 
    TRUCK = 3 
} 

그리고 클래스는 다음과 같이 정의된다 :

[Table("samples")] 
public partial class Sample 
{ 
    [Column("recording_time")] 
    public DateTime RecordingTime { get; set; } 

    [Column("x_position")] 
    public double X_Position { get; set; } 

    [Column("x_velocity")] 
    public double X_Velocity { get; set; } 

    [Column("y_position")] 
    public double Y_Position { get; set; } 

    [Column("y_velocity")] 
    public double Y_Velocity { get; set; } 

    [Key] 
    [Column("id")] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public ulong Id { get; set; } 

    [Column("target_type")] // <--- This is the offending column 
    public TargetType TargetType { get; set; } 

} 

EDIT 2 : 삽입 체와 작업 예를 조정 하였다. 사용의

그림 :

using Npgsql; 
using Dapper.FastCrud; 
... 

NpgsqlConnection.MapEnumGlobally<TargetType>("public.targettype"); // ... (1) 

OrmConfiguration.DefaultDialect = SqlDialect.PostgreSql; 

(using NpgsqlConnection conn = ...) // Connect to database 
{ 
    var samples = conn.Find<Sample>(); // <--- This works correctly 
    foreach (Sample s in samples) 
     Console.WriteLine(s); 

    ... // Generate new samples 

    using (var writer = conn.BeginBinaryImport(sql)) 
    { 
     foreach (Sample s in entities) 
     { 
      writer.StartRow(); 

      writer.Write(s.TargetType); // <--- This insert works, due to (1) 
      ... 
     } 
    } 

    foreach (Sample sample in sampleList) 
     conn.Insert<Sample>(sample); // <--- This throws PostgresException 
    ... 
} 

답변

0

당신은 아마 정수은 TargetType을 변환해야합니다.

안된하지만 뭔가 같은 :

Get 
{ 
    return (int)this.TargetType; 
} 
+0

나는 반대의 문제 같아요. 이 에러 메시지는 Npgsql이'targettype'을 기대하는 반면에'integer'를 본다는 것을 암시합니다. enum'targettype'은 postgres 데이터베이스에 정의되어 있습니다. 나는 Dapper/Npgsql에게'sample.TargetType'이'targettype' 타입임을 알릴 필요가 있다고 느낍니다. 말이 돼? – Abir

+0

http://www.npgsql.org/doc/enums_and_composites.html NpgsqlConnection.MapEnumGlobally (); 그러나 성가신 매핑없이 postgre를 사용할 수 있다면 dapper contrib을 살펴보십시오 (SQL 서버에서 가능함). – dbol

관련 문제