使用EF和SQL Azure的Azure WebJob与应用程序服务日期格式问题

时间:2016-08-15 01:44:33

标签: entity-framework azure azure-sql-database azure-web-sites azure-webjobs

我遇到了一个问题,即日期在WebJobs和App Services之间插入SQL Azure数据库的方式有所不同。

(我使用英国日期格式dd / mm / yyyy)。

我有一个存储库类TestRepository,它使用Entity Framework将记录插入到SQL Azure数据库中。其中一列是SQL datetime字段。

public class TestRepository : RepositoryBase, ITestRepository
{
    public void ProcessTestXml(string xml)
    {
        var testResultParser = new TestResultParser(xml);
        var testResults = testResultParser.TestResults;

        using (var transactionScope = new TransactionScope())
        {
            var unitOfWork = new Entities();

            var test = new Test
            {
                Id = Guid.NewGuid(),
                TestNumber = testResults.TestNumber,
                PartNumber = testResults.Part,
                SerialNumber = testResults.SerialNumber,
                Started = testResults.StartDate
            };
            unitOfWork.Tests.Add(test);

            unitOfWork.SaveChanges();
            transactionScope.Complete();
        }
    }
}

TestResultParser.TestResults属于这种类型:

public class TestResult
{
    public int TestNumber { get; set; }
    public string Part { get; set; }
    public string SerialNumber { get; set; }
    public DateTime StartDate { get; set; }
}

这是EF POCO和代码第一张地图:

public partial class Test
{
    public System.Guid Id { get; set; }
    public byte[] Timestamp { get; set; }
    [Index]
    public DateTime Started { get; set; }
    public string PartNumber { get; set; }
    public string TestNumber { get; set; }
    public string SerialNumber { get; set; }
}

public class TestMap : EntityTypeConfiguration<Test>
{
    public TestMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Id);

        this.Property(t => t.Timestamp)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();

        this.Property(t => t.PartNumber)
            .HasMaxLength(50);

        this.Property(t => t.TestNumber)
            .HasMaxLength(50);

        this.Property(t => t.SerialNumber)
            .HasMaxLength(50);
    }
}

Started字段的SQL列类型为datetime

解析器构建TestResult类。您会注意到StartDate属性是DateTime。解析器正常工作 - 日期始终在此字段中。

当我使用存储库类从我的App Service(MVC)中添加记录时,一切正常。日期输入正确。但是,当相同的存储库类托管在连接到相同 App Service插槽的WebJob中时,日期最终会以美国格式(mm / dd / yy)结束。

作为额外的奖励,如果日期在该格式中无效(例如13/01/16),那么奇迹般地恢复为英国格式!我知道,你无法弥补。

如果我在我的PC上的普通控制台应用程序中托管存储库类,并使用本地连接字符串,一切正常。并且连接字符串指向Azure数据库,它仍然可以正常工作。但是,只要我在云中的WebJob中托管存储库,它就会展示所提到的行为。

所有想法?

0 个答案:

没有答案