henkmollema / Dommel

CRUD operations with Dapper made simple.
MIT License
634 stars 100 forks source link

Can not retrieve any entity if it has column of "timestamp" type in database #240

Closed enterpub closed 1 year ago

enterpub commented 4 years ago

.NET Core 3.1 Win 10 64 bit Dommel 2.1.0 Dapper 2.0.35

Hi, after upgrading to Dommel 2.1.0 from 1.11.0 I can not retrieve any entity if it has column of timestamp type in database. For example, there is a table in database:

CREATE TABLE dbo.Charge (
  Id bigint IDENTITY(1, 1) NOT NULL,
  Description nvarchar(512) NULL,
  Amount decimal(18, 2) NOT NULL,
  RowVersion timestamp NOT NULL,
  CONSTRAINT PK_Charge PRIMARY KEY CLUSTERED (Id)
)
ON [PRIMARY]
GO

I have a related class:

namespace MyCustom
{
    using System;

    public sealed class ChargeDb
    {
        public long Id { get; set; }

        public string Description { get; set; }

        public decimal Amount { get; set; }

        public byte[] RowVersion { get; }
    }
}

Whenever I try to retrieve the ChargeDb instance it throws an exception:

An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null. (Parameter 'meth')
System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, MethodInfo meth)
Dapper.SqlMapper.GenerateDeserializerFromMap(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing, ILGenerator il) in SqlMapper.cs
Dapper.SqlMapper.GetTypeDeserializerImpl(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing) in SqlMapper.cs
Dapper.SqlMapper+TypeDeserializerCache.GetReader(IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing) in SqlMapper.TypeDeserializerCache.cs
Dapper.SqlMapper+TypeDeserializerCache.GetReader(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing) in SqlMapper.TypeDeserializerCache.cs
Dapper.SqlMapper.GetTypeDeserializer(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing) in SqlMapper.cs
Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing) in SqlMapper.cs
Dapper.SqlMapper+GridReader.ReadImpl<T>(Type type, bool buffered) in SqlMapper.GridReader.cs
MyCustom.Repositories.Impl.DapperChargeRepository.GetCharge(long chargeId) in DapperChargeRepository.cs

If I delete the RowVersion property It works as expected. Means do this:

namespace MyCustom
{
    using System;

    public sealed class ChargeDb
    {
        public long Id { get; set; }

        public string Description { get; set; }

        public decimal Amount { get; set; }

        ////public byte[] RowVersion { get; }
    }
}

Could you take a look? Please, ask any questions, I'll respond quickly

enterpub commented 4 years ago

P. S. I do not think the issue is in Dapper library itself, because I used Dapper 2.0.35 with Dommel 1.11.0 before for many months and had no issues. That is why I think something changed in Dommel

henkmollema commented 4 years ago

That's strange. Does using DateTime as the type for RowVersion works? That does work for MySQL but it seems that you're using SQL Server.

enterpub commented 4 years ago

Hi, I use SqlServer as you mentioned Microsoft SQL Server 2016 (SP1-GDR) (KB4505219) - 13.0.4259.0 (X64) Jun 15 2019 19:20:12 Developer Edition (64-bit) on Windows 10 Pro 6.3 (Build 18362: )

I did not try DateTime, but I will and let you know. The .net equivalent for sql server timestamp is byte[]. Event if DateTime works it would still be confusing to use it

lucaxchaves commented 3 years ago

Did you try to use TimeSpan or TimeSpan??

enterpub commented 3 years ago

@lucasloss, the mapping type for timestamp in .net is byte[]. I should not map it to TimeSpan even if it would work. timestamp has no common with time or date. It is just a confusing name of the sql sever type. SQL Server Data Type Mappings

henkmollema commented 2 years ago

@enterpub are you still facing this issue? Could be a Dapper issue? I don't see how Dommel this is related to Dommel.

enterpub commented 2 years ago

Hi @henkmollema, I'll test it when have a chance. We are still using version 1.11.0 because of that issue.

henkmollema commented 1 year ago

Closing this for now.