Oracle Entity Framework提供程序不存储DateTime.Now(以毫秒为单位) [英] Oracle Entity Framework provider doesn't store DateTime.Now with milliseconds

查看:100
本文介绍了Oracle Entity Framework提供程序不存储DateTime.Now(以毫秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上和这里的那个人有完全相同的问题.

I have basically the exact same question as this guy here.

为什么我无法保存当前的DateTime.Now使用实体框架

但是他使用的是SQL Server,而我使用的是Oracle. (我的应用程序必须同时使用)

But he was using SQL Server, and I am using Oracle. (My application must work with both)

他的问题是在数据库级别没有正确设置精度.

His problem was that precision wasn't set correctly at the db level.

我注意到,如果我在oracle数据库中手动编辑毫秒,EF可以拉出正确的时间戳(以毫秒为单位).但是,当我创建一个带有DateTime属性为"DateTime.Now"的实体时,该实体将被截断.

I've noticed that if I manually edit the milliseconds in my oracle database, EF can pull out the correct timestamp with milliseconds. But when I create an Entity with a DateTime property to "DateTime.Now" it gets truncated.

DateColumn1属性为Oracle类型Timestamp

The DateColumn1 attribute is of the Oracle type Timestamp

我记录了插入语句

insert into "SchemaName"."TableName"("DateColumn1") values (:P0) --:P0:'5/14/2015 4:07:27 PM' (Type = Date)

insert into "SchemaName"."TableName"("DateColumn1") values (:P0) --:P0:'5/14/2015 4:07:27 PM' (Type = Date)

疯狂的是,它可以在SQL Server中工作.

The crazy thing is that this works in SQL Server.

推荐答案

啊!我的同事真是个好主意,而且行得通!

Aha! My awesome colleague had an idea and it worked!

在我们的EF代码中,我们尝试放置

In our EF code we tried putting

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<EntityClass>().Property(p => p.TIMESTAMP).HasPrecision(6);
}

然后将毫秒级的DateTime.Now存储到数据库中

And then the DateTime.Now with milliseconds got stored into the database

  1. 我的应用程序必须同时使用SQL Server和Oracle.所以...
  2. 我首先在EDMX图表中设计数据库
  3. 完成图后,我为SQL Server生成了DDL.
  4. 由于某种原因,Oracle EF提供程序无法生成DDL,所以我着手手动对SQL Server DDL进行更改,因此从语法上来说是正确的

  1. My app has to work with both SQL Server and Oracle. So...
  2. I started by designing my database in an EDMX Diagram
  3. Once the diagram was done, I generated the DDL for SQL Server.
  4. For some reason the Oracle EF provider couldn't generate the DDL so I proceeded to manually make changes to the SQL Server DDL so it would be correct syntactically

第一个问题-我的Oracle DDL使用的是日期而不是时间戳.确保您使用时间戳记!!! Oracle中的DateTime不存储毫秒.

1st Problem - my Oracle DDL was using a Date instead of Timestamp. Make sure you use Timestamp!!! DateTime in Oracle doesn't store milliseconds.

首先使用数据库中的代码作为实际解决方案

  1. 我希望该应用使用代码优先"方法(只是我的偏爱.我认为它更易于维护)
  2. 因此,我连接到SQL Server数据库并从该架构生成了所有类.
  3. 我通过了所有单元测试,然后决定使用Oracle数据库进行测试
  4. 即使从DATE更改为Timestamp,我仍然遇到毫秒数的问题.
  5. 我在测试视觉工作室解决方案中使用Oracle中的TIMESTAMP(6)类型生成了另一个代码优先"模型,除了当我查看OnModelCreating代码时,它没有使用HasPrecision(6)生成任何东西,也没有任何装饰器.生成的C#POCO类中的属性.
  6. 我注意到,如果您的OnModelCreating中包含HasPrecision(6)代码,则代码优先级CreateDatabase()实际上将构成一个Oracle TIMESTAMP(6).如果您不这样做,那么Oracle EF提供程序将使用DATE
  1. I wanted the app to use the Code First approach (Just my preference. I think it's easier to maintain)
  2. So I connected to the SQL Server database and generated all of my classes from that schema.
  3. I got all of my unit tests passing and then decided to test it with the Oracle database
  4. Even after changing from DATE to Timestamp, I was still having problems with the milliseconds going in.
  5. I generated another Code First model in a test visual studio solution with a TIMESTAMP(6) type in Oracle, except when I looked at the OnModelCreating code, it did not generate anything with HasPrecision(6) nor were there any decorators on the property in the generated C# POCO class.
  6. I noticed if you have the HasPrecision(6) code in your OnModelCreating, the Code FirstCreateDatabase() will actually make an Oracle TIMESTAMP(6). If you don't, then the Oracle EF provider will use DATE

我认为,如果您采用模型优先"方法,则可以在EDMX图表中设置精度值,但是我听说这是不好的做法.

I think if you do the Model First approach you can set precision values in the EDMX diagram, but I've heard that it's bad practice.

这篇关于Oracle Entity Framework提供程序不存储DateTime.Now(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆