首页 > 解决方案 > Fluent-API 的自定义 DataAnnotations 属性

问题描述

我正在将一个项目从 EF6 移植到 EF7 Core,并且我正在将所有带有 DataAnnotationtions 的类转换为 FluentAPI。

在项目中,我广泛使用了自定义数据注释属性。我需要转换:

    [Required, MaxLength(Consts.DbLength.OriginName)]
    [FrontendEditable]
    public string OriginName { get; set; }

其中 [FrontendEditable] 类似于:

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
public class FrontendEditableAttribute : Attribute
{
    public FrontendEditableAttribute()
    {
    }

我想将其转换为 modelBuilder 中的 Fluent API,例如:

            modelBuilder.Entity<Origin>(e =>
        {
            e.Property(oN => oN.OriginName).IsRequired();
            e.Property(oN => oN.OriginName).HasColumnType("NVARCHAR");
            e.Property(oN => oN.OriginName).HasMaxLength(50);
        });

这是为了使 db 的某些字段仅可从 FrontEnd 编辑并锁定。

任何建议将不胜感激。

标签: entity-framework-coredata-annotationsef-fluent-api

解决方案


推荐阅读