sapiens / SqlFu

Fast and versatile .net core data mapper/micro-orm
Other
229 stars 50 forks source link

Timestamp seems to be broken v2.3.7 #79

Closed Bitfiddler closed 8 years ago

Bitfiddler commented 8 years ago

In previous versions of SqlFu, a timestamp datatype from SqlServer mapped onto a Datetime in c#. This appears to no longer be the case but I cannot find any docs on what the new mapping is. In the code below, if I comment out the RowVersion the unit test passes, if I uncomment it I get a cast exception.

Here is my unit test: `

public class ExchangeTradeFileTests : nspec
{
    using System;
    using System.Data;
    using NSpec;
    using SqlFu;
    using MyApp.Domain;
    public void when_fetching_existing_tradefiles()
    {
        ExchangeFile file = null;

        before = () =>
        {
            SqlFuDao.ConnectionStringIs("Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True", DbEngine.SqlServer);
            SqlFuDao.OnCommand = cmd => Console.WriteLine(cmd.FormatCommand());
            SqlFuDao.OnException = (cmd, ex) => Console.WriteLine("\nSql:{1}\nException:\n\t\t{0}", ex, cmd.FormatCommand());
        };

        act = () =>
        {
            using (var db = SqlFuDao.GetConnection())
            {
                file = db.Get<ExchangeFile>(x => x.FileName == "myTestFileName.txt.zip");
            }
        };

        it["it should fetch a record without errors"] = () =>
        {
            file.should_not_be_null();
        };
    }
}

`

Here is my Domain object: `

    [Table("ExchangeFiles", PrimaryKey = "Id", AutoGenerated = false)]
    public class ExchangeFile : IBasicEntity
    {
        using System;
        using Common.EntityFramework.Utilities;
        using SqlFu;

        // Common to all tables
        public ExchangeFile()
        {
            Id = Id == Guid.Empty ? ContextTools.GenerateComb() : Id;
        }

        public Guid Id { get; set; }
        [QueryOnly]
        public int RowNumber { get; set; }
        //        [QueryOnly]
        //        public DateTime RowVersion { get; set; }

        public string FileName { get; set; }
        public bool ImportSuccess { get; set; }
        public DateTime DateImported { get; set; }
    }

`

Bitfiddler commented 8 years ago

Using other ORMs I have used as a basis for a stab in the dark, I found that a byte array works as a mapping for timestamp. So:

public byte[] RowVersion {get;set;}

Seems to work fine. Might be handy to add this to the main page of examples.

sapiens commented 8 years ago

You're right. To quote from MSDN

A nonnullable rowversion column is semantically equivalent to a binary(8) column.

I'll keep this in mind for the docs, right now I'm working to release SqlFu v3.

sapiens commented 8 years ago

Btw, what's with the version number in the title? Did you mean 2.3.5?

Bitfiddler commented 8 years ago

Updated the version in the title, must have inadvertently had another nuget package selected while I was looking at the version number ;-)