简体   繁体   English

使用EF Core处理MySQL零日期

[英]Handling MySQL Zero Date with EF Core

Background: I have 200+ legacy VB6 apps which I am converting to C#, using EntityFramework Core as our ORM. 背景:我有200多个传统的VB6应用程序,我将转换为C#,使用EntityFramework Core作为我们的ORM。 Unfortunately a lot of the apps utilize MySQL's zero date (0000-00-00) . 不幸的是,许多应用程序使用MySQL的零日期(0000-00-00) So I need to cater for this and be able to store a zero date in some way. 所以我需要迎合这一点,并能够以某种方式存储零日期。 Changing the fundamental way this works in the 200+ apps is not currently an option. 在200多个应用程序中更改此功能的基本方式目前不是一种选择。

Setup: I can define an entity property which represents the field definition in MySQL eg: 设置:我可以定义一个实体属性,表示MySQL中的字段定义,例如:

 public DateTime ExpiryDate { get; set; }

...

 entity.Property(e => e.ExpiryDate)
                       .IsRequired()
                       .HasColumnType("datetime")
                       .HasDefaultValueSql("'0000-00-00 00:00:00'");

This will correctly store a zero date if no value is sent on an insert. 如果在插入上没有发送任何值,这将正确存储零日期。

Problem: Because C# has a minimum date of 0001-01-01 it is not possible for me to explicitly store a zero date. 问题:因为C#的最小日期为0001-01-01所以我无法明确存储零日期。 So my question is... Is there a way to set up my entities to get this zero date into and out of the database?? 所以我的问题是......有没有办法设置我的实体来进入和退出数据库?

So far: I have tried using a backing field, defined as a string so that I can manipulate any DateTime.MinValue to become '0000-00-00' . 到目前为止:我尝试使用支持字段,定义为字符串,以便我可以操纵任何DateTime.MinValue成为'0000-00-00' This allows me to store the zero date but then causes a casting issue (as you would expect) when trying to retrieve the data: 这允许我存储零日期,但在尝试检索数据时会导致转换问题(正如您所料):

System.InvalidCastException : Unable to cast object of type 'System.DateTime' to type 'System.String'. System.InvalidCastException:无法将类型为“System.DateTime”的对象强制转换为“System.String”。

Current packages I am using are: 我目前使用的包是:

  • EFCore 1.1 EFCore 1.1
  • PomeloEntityFrameWorkCore 1.1.2 PomeloEntityFrameWorkCore 1.1.2
  • MySQL 5.7.18 MySQL 5.7.18

People might argue that this is better suited as a comment, but basically, it's to long for that. 人们可能会争辩说这更适合作为评论,但基本上,这是为了解决这个问题。

And: 和:

You'll have to help me out a bit since I don't have a working system at hand, so I am doing this from the top of my head. 你必须帮我一点,因为我手头没有工作系统,所以我从头到尾这样做。 (and I am in a bit of a rush) (而且我有点匆忙)

First, start out with a not mapped property: 首先,从未映射的属性开始:

[NotMapped]
public DateTime ExpiryDate { get; set; }

This property is not mapped. 此属性未映射。 It might lead to some errors concerning the database does not match the model, but we can overcome that. 它可能导致有关数据库的一些错误与模型不匹配,但我们可以克服这一点。 This property will not be automatically filled when querying the data. 查询数据时,不会自动填充此属性。 So, we need a way to deal with this our self. 所以,我们需要一种方法来处理这个问题。

For example, ( which is a bad example because we need the context in the entity somewhere ): 例如,( 这是一个不好的例子,因为我们需要实体中的某个上下文 ):

[NotMapped]
public DateTime? ExpiryDate 
{
    get
    {
         //of course you'll need some caching here
         var s = context.Database.SqlQuery<string>("query to select datetime as string");
         //additional logic to determine validity:
         if (s == "0000-00-00")
             return null;
         //else:
         //do the conversion
    }
 }

The basic question here; 这里的基本问题; how far do you want to go to support this within EF framework? 你想在EF框架内支持多远? Do you only need to read it, or write as well, using the change tracker of EF etc.? 您是否只需要使用EF等的更改跟踪器来阅读或写入?

There are other posibilities, for example, to perform a perform a CAST to nvarchar within the SQL itself to obtain the data and further process it. 还有其他可能性,例如,在SQL本身内执行CAST到nvarchar以获取数据并进一步处理它。

Maybe the ModelBuilder exposes some additional options. 也许ModelBuilder公开了一些额外的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM