如何将CLR属性类型的所有实例映射到EF Core中的列类型?

时间:2017-02-08 07:30:15

标签: entity-framework entity-framework-6 entity-framework-core

在EF 6中我会在OnModelCreating中使用它:

modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("date"));

但即使经过大约两个小时的谷歌搜索,我也无法找到EF Core替代品。

可以吗?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:2)

您可以在EF Core中创建“自定义约定”,如下所示:

        protected override void OnModelCreating(ModelBuilder builder)
        {
            foreach (var entity in builder.Model.GetEntityTypes())
            {
                foreach (var property in entity.GetProperties())
                {
                    if (property.ClrType == typeof(DateTime))
                    {
                        property.Relational().ColumnType = "date";
                    }
                }
            }
        }

答案 1 :(得分:-1)

我认为你正在寻找的东西很接近,但我找到的例子要求你指定Model和model属性

return (s1.length() == 0) ? "hello" : (s2.length() == 0 ? "goodbye" : "something when none is satisfied");

以下是doc和GitHub源代码的链接:

https://docs.microsoft.com/en-us/ef/core/modeling/relational/data-types

https://github.com/aspnet/EntityFramework.Docs/blob/master/samples/core/Modeling/FluentAPI/Samples/Relational/DataTypeForProvider.cs