EF:6 - 如果值在数据库中设置了默认值,如何跳过值的插入?

EF:6 - How to skip insertion of value if value has default value set in DB?

我正在与 Entity Framework 6 和 MySQl 数据库

抗争

我一切正常,但我对日期或非强制性值感到困惑。

在我的数据库中,在 "Users" table 中,我有列 "RegistrationDate",其默认值为 "CURRENT_TIMESTAMP" 这是什么意思,如果在插入时未提供值,它将插入默认值 = 服务器的日期时间

我将我的架构反向工程到 C# 中并且一切正常,但是当我插入 "User" 而不将日期设置为 "RegistrationDate" 属性 时,它将新日期插入到数据库中“0001-01-01 00:00:00”并忽略 "CURRENT_TIMESTAMP".

所以我想知道如何将其设置为忽略 "RegistrationDate" 并且如果没有专门设置为某个日期则不向数据库中插入任何内容?

我认为我们中的许多人在处理 EF 时都被默认数据库值所困扰 - 它没有考虑到它们(对此有很多问题 - 例如 Entity Framework - default values doesn't set in sql server table

我想说的是,如果您没有明确设置日期时间并希望它为空,则需要在代码中完成。

我猜测 SQL EF 生成的是设置字段值。即使你不在代码中设置,EF也不知道数据库有默认值,也不知道他应该忽略。

This article,从 2011 年开始,说有一个 DatabaseGenerated 属性,您可以这样使用它:

[DatabaseGenerated(DatabaseGenerationOption.Computed)]
public DateTime RegistrationDate { get; set; }

因此,EF 现在知道它应该在您查询数据库时检索数据,但应该依赖数据库来设置值。

但是,我不知道如果您显式设置该值,它会做什么。也许它会忽略它,这可能不是你真正想要的。

我没有测试它,这只是一个猜测,但在我看来这是一个不错的解决方案。

[Edit1] 几个月前,我看到 this video,那家伙在他的 DbContext class 中做了类似的事情(我相信你有它)在49:12(视频是葡萄牙语)(我修改了代码,但没有测试):

//This method will be called for every change you do - performance may be a concern
public override int SaveChanges()
{
    //Every entity that has a particular property
    foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("YourDateField") != null))
    {

        if (entry.State == EntityState.Added)
        {
            var date = entry.Property("YourDateField");

            //I guess that if it's 0001-01-01 00:00:00, you want it to be DateTime.Now, right?
            //Of course you may want to verify if the value really is a DateTime - but for the sake of brevity, I wont.
            if (date.CurrentValue == default(DateTime))
            {
                date.CurrentValue = DateTime.Now;
            }   
            else //else what? 
            {

                //Well, you don't really want to change this. It's the value you have set. But i'll leave it so you can see that the possibilities are infinite!

            }
        }


        if (entry.State == EntryState.Modified)
        {
            //If it's modified, maybe you want to do the same thing.

            //It's up to you, I would verify if the field has been set (with the default value cheking)
            //and if it hasn't been set, I would add this:

            date.IsModified = false;

            //So EF would ignore it on the update SQL statement.

        }

    }



}