简体   繁体   English

覆盖派生类中的 NotMapped 属性

[英]Override NotMapped attribute in derived class

I'm working on an EF Code-First based application.我正在开发基于 EF Code-First 的应用程序。 I have a base class inherited by dozens of classes, each representing entities in database.我有一个由几十个类继承的基类,每个类都代表数据库中的实体。 The base class has a property with [NotMapped] attribute, that was originally required to be [NotMapped] for all derived classes as well.基类有一个带有[NotMapped]属性的属性,对于所有派生类,最初也要求该属性为[NotMapped]

public class BaseEntity
{
    [NotMapped]
    public string Message { get; set; }
}

I've come across an entity having column name exactly as that property name, but just due to inheriting [NotMapped] attribute from the parent, the value doesn't get stored in database.我遇到了一个列名与该属性名完全相同的实体,但仅仅由于从父级继承[NotMapped]属性,该值不会存储在数据库中。

public class InheritedEntity : BaseEntity
{
    public string Message { get; set; } // This is what I want mapped
}

Is there any way to override the NotMapped behaviour just for that class, either through DataAnnotations or FluentAPI?有什么方法可以通过 DataAnnotations 或 FluentAPI 覆盖NotMapped行为? I've tried setting [Column()] but it doesn't work.我试过设置[Column()]但它不起作用。

You need to create a new attribute that does what NotMapped do.您需要创建一个新属性来执行 NotMapped 的功能。

NotMappedPropertyAttributeConvention
Applies the ff to your modelBuilder configuration: 将 ff 应用于您的模型构建器配置:

public class CustomNotMappedAttribute : Attribute
{
    public CustomNotMappedAttribute()
    {
        this.Ignore = true;
    }
    public bool Ignore { get; set; }
}

First, create custom attribute.首先,创建自定义属性。


public class CustomNotMappedPropertyAttributeConvention : PropertyAttributeConfigurationConvention
{
    public override void Apply(PropertyInfo memberInfo, ConventionTypeConfiguration configuration, CustomNotMappedAttribute attribute)
    {
        if (attribute.Ignore)
        {
            configuration.Ignore(memberInfo);
        }
    }
}

Then, create a PropertyAttributeConvention然后,创建一个 PropertyAttributeConvention


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Add(new CustomNotMappedPropertyAttributeConvention());
}

Then, add it to the configuration conventions然后,将其添加到配置约定中

[CustomNotMapped]

Your property in entitybase should be decorated with:您在 entitybase 中的属性应装饰为:


public class BaseEntity
{
    [CustomNotMapped]
    public virtual string Message { get; set; }
}

public class InheritedEntity : BaseEntity
{
    [CustomNotMapped(Ignore = false)]
    public override string Message { get; set; }
}

instead of 而不是
[NotMapped]
 public class BaseEntity { [CustomNotMapped] public virtual string Message { get; set; } } public class InheritedEntity : BaseEntity { [CustomNotMapped(Ignore = false)] public override string Message { get; set; } }

There you go.你去吧。 Your message property in your BaseEntity will be ignore except to those decorated with [CustomNotMapped(Ignore = false)]除了用[CustomNotMapped(Ignore = false)]装饰的属性之外,您的 BaseEntity 中的消息属性将被忽略

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

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