Entity framework Azure WebJob vs应用程序服务日期格式问题与EF和SQL Azure
我偶然发现了一个问题,即WebJobs和App服务之间在将日期插入sqlazure数据库的方式上存在差异 (我使用的是英国日期格式dd/mm/yyyy) 我有一个存储库类Entity framework Azure WebJob vs应用程序服务日期格式问题与EF和SQL Azure,entity-framework,azure,azure-sql-database,azure-web-app-service,azure-webjobs,Entity Framework,Azure,Azure Sql Database,Azure Web App Service,Azure Webjobs,我偶然发现了一个问题,即WebJobs和App服务之间在将日期插入sqlazure数据库的方式上存在差异 (我使用的是英国日期格式dd/mm/yyyy) 我有一个存储库类TestRepository,它使用实体框架将记录插入到sqlazure数据库中。其中一列是SQLdatetime字段 public class TestRepository : RepositoryBase, ITestRepository { public void ProcessTestXml(string xml
TestRepository
,它使用实体框架将记录插入到sqlazure数据库中。其中一列是SQLdatetime
字段
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);
}
}
公共部分类测试
{
public System.Guid Id{get;set;}
公共字节[]时间戳{get;set;}
[索引]
公共日期时间已开始{get;set;}
公共字符串零件号{get;set;}
公共字符串TestNumber{get;set;}
公共字符串序列号{get;set;}
}
公共类TestMap:EntityTypeConfiguration
{
公共测试图()
{
//主键
this.HasKey(t=>t.Id);
//性质
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。解析器工作正常-此字段中的日期始终正确
当我使用repository类从我的应用程序服务(MVC)中添加记录时,一切正常。日期输入正确。但是,当相同的存储库类托管在连接到相同的应用程序服务槽的WebJob中时,日期以美国格式(mm/dd/yy)结束
作为额外的奖励,如果该日期在该格式(如13/01/16)中无效,则奇迹般地恢复为英国格式!我知道,你无法弥补
如果我在PC上的普通控制台应用程序中使用本地连接字符串托管repository类,则一切正常。由于连接字符串指向Azure数据库,它仍然可以正常工作。但是,一旦我将存储库托管在云中的WebJob中,它就会表现出上述行为
有什么想法吗?你在MVC网站和webjob上使用相同的webapp吗?是的!webjob附加到同一个MVC应用程序。请发布您的代码好吗?添加了代码,更新了我的问题。谢谢。我已经测试过使用EF将日期时间保存到Azure sql数据库,Azure Web app和webjobs都保存了正确的日期时间。如果更改系统本地,则输出日期时间将不同。如果您的web应用程序和web作业使用相同的数据库,您的数据库中是否有两种格式的datetime?您是否对MVC网站和web作业使用相同的web应用程序?是!webjob附加到同一个MVC应用程序。请发布您的代码好吗?添加了代码,更新了我的问题。谢谢。我已经测试过使用EF将日期时间保存到Azure sql数据库,Azure Web app和webjobs都保存了正确的日期时间。如果更改系统本地,则输出日期时间将不同。如果您的web应用程序和webjobs使用相同的数据库,您的意思是数据库中将有两种格式的datetime吗?