简体   繁体   English

在实体接口(EF Core)中声明要忽略的属性

[英]Declare properties to ignore in entities interface (EF Core)

EF Core 2.2英孚核心 2.2

Having this interface:有这个界面:

public interface INotPersistingProperties
{
   string MyNotPersistingPropertyA { get; set; }
   string MyNotPersistingPropertyB  { get; set; }
}

and a lot of entities that implements the interface以及很多实现接口的实体

public class MyEntity : INotPersistingProperties
{
  public int Id { get; set; }
  public string MyNotPersistingPropertyA { get; set; }
  public string MyNotPersistingPropertyB { get; set; }
}

is there any chance to automatically ignore, for all entities that implement the INotPersistingProperties, those properties (using Fluent API)?对于所有实现 INotPersistingProperties 的实体,是否有机会自动忽略这些属性(使用 Fluent API)?

Currently EF Core does not provide a way to define custom conventions, but you can add the following to your OnModelCreating override (after all entity types are discovered) to iterate all entity types implementing the interface and call Ignore fluent API for each property:目前 EF Core 不提供定义自定义约定的方法,但您可以将以下内容添加到OnModelCreating覆盖(在发现所有实体类型之后)以迭代实现接口的所有实体类型并为每个属性调用Ignore fluent API:

var propertyNames = typeof(INotPersistingProperties).GetProperties()
    .Select(p => p.Name)
    .ToList();
var entityTypes = modelBuilder.Model.GetEntityTypes()
    .Where(t => typeof(INotPersistingProperties).IsAssignableFrom(t.ClrType));
foreach (var entityType in entityTypes)
{
    var entityTypeBuilder = modelBuilder.Entity(entityType.ClrType);
    foreach (var propertyName in propertyNames)
        entityTypeBuilder.Ignore(propertyName);
}

To ignore all classes of specific interface for EF Core I used this code:为了忽略 EF Core 特定接口的所有类,我使用了以下代码:

protected override void OnModelCreating(ModelBuilder builder)
        {

         var multitenantTypes = typeof(IMultiTenantEntity)
            .Assembly
            .GetTypes()
            .Where(x => typeof(IMultiTenantEntity).IsAssignableFrom(x))
            .ToArray();

        foreach (var typeToIgnore in multitenantTypes)
        {
            builder.Ignore(typeToIgnore);
        }

    }

You can put NotMapped on the properties in the interface and then use MetadataType attribute您可以将NotMapped放在界面中的属性上,然后使用MetadataType 属性

public interface INotPersistingProperties
{
   [NotMapped]
   string MyNotPersistingPropertyA { get; set; }
   [NotMapped]
   string MyNotPersistingPropertyB  { get; set; }
}

[MetadataType(typeof(INotPersistingProperties))]
public class MyEntity : INotPersistingProperties
{
  public int Id { get; set; }
  public string MyNotPersistingPropertyA { get; set; }
  public string MyNotPersistingPropertyB { get; set; }
}

or you can create a base class and put attribute NotMapped on your properties或者您可以创建一个基类并将属性NotMapped放在您的属性上

You can use the NotMapped attribute on such properties in the model class.您可以在模型类中的NotMapped属性上使用NotMapped属性。

Or you can ignore the properties using reflection in DBContext class's OnModelCreating method like below.或者您可以在 DBContext 类的OnModelCreating方法中使用反射忽略属性,如下所示。

foreach(var property in typeof(INotPersistingProperties).GetProperties())
    modelBuilder.Types().Configure(m => m.Ignore(property.Name));

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

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